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.
21 #include <sys/types.h>
27 #include "libmemcached/common.h"
30 #include "clients/generator.h"
31 #include "clients/execute.h"
33 #define SMALL_STRING_LEN 1024
38 #ifdef HAVE_LIBMEMCACHEDUTIL
40 #include "libmemcached/memcached_util.h"
43 #include "hash_results.h"
45 #define GLOBAL_COUNT 10000
46 #define GLOBAL2_COUNT 100
47 #define SERVERS_TO_CREATE 5
48 static uint32_t global_count
;
50 static pairs_st
*global_pairs
;
51 static const char *global_keys
[GLOBAL_COUNT
];
52 static size_t global_keys_length
[GLOBAL_COUNT
];
55 static test_return_t
pre_binary(memcached_st
*memc
);
58 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
62 (void)memcached_create(&memc
);
63 memcached_free(&memc
);
68 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
70 memcached_server_st
*server_list
;
71 memcached_return_t rc
;
73 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
74 test_true(server_list
== NULL
);
76 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
77 test_true(server_list
== NULL
);
79 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
80 test_true(server_list
== NULL
);
85 #define TEST_PORT_COUNT 7
86 in_port_t test_ports
[TEST_PORT_COUNT
];
88 static memcached_return_t
server_display_function(const memcached_st
*ptr
__attribute__((unused
)),
89 const memcached_server_st
*server
,
93 size_t bigger
= *((size_t *)(context
));
94 assert(bigger
<= memcached_server_port(server
));
95 *((size_t *)(context
))= memcached_server_port(server
);
97 return MEMCACHED_SUCCESS
;
100 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
102 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
104 memcached_return_t rc
;
105 memcached_server_fn callbacks
[1];
106 memcached_st
*local_memc
;
108 local_memc
= memcached_create(NULL
);
109 test_true(local_memc
);
110 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
112 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
114 test_ports
[x
]= (in_port_t
)random() % 64000;
115 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
116 test_true(memcached_server_count(local_memc
) == x
+ 1);
118 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
120 test_true(rc
== MEMCACHED_SUCCESS
);
123 callbacks
[0]= server_display_function
;
124 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
127 memcached_free(local_memc
);
132 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
134 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
135 memcached_return_t rc
;
136 memcached_server_fn callbacks
[1];
137 memcached_st
*local_memc
;
138 memcached_server_instance_st instance
;
140 local_memc
= memcached_create(NULL
);
141 test_true(local_memc
);
142 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
143 test_true(rc
== MEMCACHED_SUCCESS
);
145 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
146 test_true(rc
== MEMCACHED_SUCCESS
);
147 instance
= memcached_server_instance_by_position(local_memc
, 0);
148 test_true(memcached_server_port(instance
) == 43043);
150 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
151 test_true(rc
== MEMCACHED_SUCCESS
);
153 instance
= memcached_server_instance_by_position(local_memc
, 0);
154 test_true(memcached_server_port(instance
) == 43042);
156 instance
= memcached_server_instance_by_position(local_memc
, 1);
157 test_true(memcached_server_port(instance
) == 43043);
159 callbacks
[0]= server_display_function
;
160 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
163 memcached_free(local_memc
);
168 static memcached_return_t
server_print_callback(const memcached_st
*ptr
__attribute__((unused
)),
169 const memcached_server_st
*server
,
170 void *context
__attribute__((unused
)))
172 (void)server
; // Just in case we aren't printing.
175 fprintf(stderr
, "%s(%d)", memcached_server_name(server
), memcached_server_port(server
));
178 return MEMCACHED_SUCCESS
;
181 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
__attribute__((unused
)))
183 memcached_return_t rc
;
184 memcached_st local_memc
;
186 memcached_server_st
*servers
;
187 memcached_server_fn callbacks
[1];
189 const char *server_string
= "localhost:4444, localhost:4445, localhost:4446, localhost:4447, localhost, 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";
191 memc
= memcached_create(&local_memc
);
193 servers
= memcached_servers_parse(server_string
);
195 rc
= memcached_server_push(memc
, servers
);
196 memcached_server_list_free(servers
);
198 callbacks
[0]= server_print_callback
;
199 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
201 memcached_free(memc
);
206 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
__attribute__((unused
)),
207 const memcached_server_st
*server
,
211 uint32_t x
= *((uint32_t *)(context
));
213 assert(test_ports
[x
] == server
->port
);
214 *((uint32_t *)(context
))= ++x
;
216 return MEMCACHED_SUCCESS
;
219 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
221 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
222 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
223 memcached_return_t rc
;
224 memcached_server_fn callbacks
[1];
225 memcached_st
*local_memc
;
227 local_memc
= memcached_create(NULL
);
228 test_true(local_memc
);
230 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
232 test_ports
[x
]= (in_port_t
)(random() % 64000);
233 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
234 test_true(memcached_server_count(local_memc
) == x
+1);
236 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
238 test_true(rc
== MEMCACHED_SUCCESS
);
241 callbacks
[0]= server_display_unsort_function
;
242 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
244 /* Now we sort old data! */
245 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
246 callbacks
[0]= server_display_function
;
247 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
250 memcached_free(local_memc
);
255 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
258 memc
= memcached_create(NULL
);
260 memcached_free(memc
);
265 static test_return_t
clone_test(memcached_st
*memc
)
269 memcached_st
*memc_clone
;
270 memc_clone
= memcached_clone(NULL
, NULL
);
271 test_true(memc_clone
);
272 memcached_free(memc_clone
);
275 /* Can we init from null? */
277 memcached_st
*memc_clone
;
278 memc_clone
= memcached_clone(NULL
, memc
);
279 test_true(memc_clone
);
282 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
283 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
284 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
285 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
288 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
289 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
290 test_true(memc_clone
->distribution
== memc
->distribution
);
291 { // Test all of the flags
292 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
293 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
294 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
295 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
296 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
297 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
298 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
299 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
300 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
301 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
302 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
303 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
304 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
305 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
306 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
308 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
309 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
310 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
311 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
312 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
313 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
314 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
315 test_true(memc_clone
->on_clone
== memc
->on_clone
);
316 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
317 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
318 test_true(memc_clone
->recv_size
== memc
->recv_size
);
319 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
320 test_true(memc_clone
->send_size
== memc
->send_size
);
321 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
322 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
323 test_true(memc_clone
->user_data
== memc
->user_data
);
325 memcached_free(memc_clone
);
328 /* Can we init from struct? */
330 memcached_st declared_clone
;
331 memcached_st
*memc_clone
;
332 memset(&declared_clone
, 0 , sizeof(memcached_st
));
333 memc_clone
= memcached_clone(&declared_clone
, NULL
);
334 test_true(memc_clone
);
335 memcached_free(memc_clone
);
338 /* Can we init from struct? */
340 memcached_st declared_clone
;
341 memcached_st
*memc_clone
;
342 memset(&declared_clone
, 0 , sizeof(memcached_st
));
343 memc_clone
= memcached_clone(&declared_clone
, memc
);
344 test_true(memc_clone
);
345 memcached_free(memc_clone
);
351 static test_return_t
userdata_test(memcached_st
*memc
)
354 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
355 test_true(memcached_get_user_data(memc
) == foo
);
356 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
361 static test_return_t
connection_test(memcached_st
*memc
)
363 memcached_return_t rc
;
365 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
366 test_true(rc
== MEMCACHED_SUCCESS
);
371 static test_return_t
error_test(memcached_st
*memc
)
373 memcached_return_t rc
;
374 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
375 982370485U, 1263635348U, 4242906218U, 3829656100U,
376 1891735253U, 334139633U, 2257084983U, 3088286104U,
377 13199785U, 2542027183U, 1097051614U, 199566778U,
378 2748246961U, 2465192557U, 1664094137U, 2405439045U,
379 1842224848U, 692413798U, 3479807801U, 919913813U,
380 4269430871U, 610793021U, 527273862U, 1437122909U,
381 2300930706U, 2943759320U, 674306647U, 2400528935U,
382 54481931U, 4186304426U, 1741088401U, 2979625118U,
383 4159057246U, 3425930182U, 2593724503U, 1868899624U,
384 1769812374U, 2302537950U, 1110330676U };
386 // You have updated the memcache_error messages but not updated docs/tests.
387 test_true(MEMCACHED_MAXIMUM_RETURN
== 43);
388 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
391 const char *msg
= memcached_strerror(memc
, rc
);
392 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
393 MEMCACHED_HASH_JENKINS
);
394 if (values
[rc
] != hash_val
)
396 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
397 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
399 test_true(values
[rc
] == hash_val
);
405 static test_return_t
set_test(memcached_st
*memc
)
407 memcached_return_t rc
;
408 const char *key
= "foo";
409 const char *value
= "when we sanitize";
411 rc
= memcached_set(memc
, key
, strlen(key
),
412 value
, strlen(value
),
413 (time_t)0, (uint32_t)0);
414 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
419 static test_return_t
append_test(memcached_st
*memc
)
421 memcached_return_t rc
;
422 const char *key
= "fig";
423 const char *in_value
= "we";
424 char *out_value
= NULL
;
428 rc
= memcached_flush(memc
, 0);
429 test_true(rc
== MEMCACHED_SUCCESS
);
431 rc
= memcached_set(memc
, key
, strlen(key
),
432 in_value
, strlen(in_value
),
433 (time_t)0, (uint32_t)0);
434 test_true(rc
== MEMCACHED_SUCCESS
);
436 rc
= memcached_append(memc
, key
, strlen(key
),
437 " the", strlen(" the"),
438 (time_t)0, (uint32_t)0);
439 test_true(rc
== MEMCACHED_SUCCESS
);
441 rc
= memcached_append(memc
, key
, strlen(key
),
442 " people", strlen(" people"),
443 (time_t)0, (uint32_t)0);
444 test_true(rc
== MEMCACHED_SUCCESS
);
446 out_value
= memcached_get(memc
, key
, strlen(key
),
447 &value_length
, &flags
, &rc
);
448 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
449 test_true(strlen("we the people") == value_length
);
450 test_true(rc
== MEMCACHED_SUCCESS
);
456 static test_return_t
append_binary_test(memcached_st
*memc
)
458 memcached_return_t rc
;
459 const char *key
= "numbers";
460 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
466 rc
= memcached_flush(memc
, 0);
467 test_true(rc
== MEMCACHED_SUCCESS
);
469 rc
= memcached_set(memc
,
472 (time_t)0, (uint32_t)0);
473 test_true(rc
== MEMCACHED_SUCCESS
);
475 for (x
= 0; store_list
[x
] ; x
++)
477 rc
= memcached_append(memc
,
479 (char *)&store_list
[x
], sizeof(uint32_t),
480 (time_t)0, (uint32_t)0);
481 test_true(rc
== MEMCACHED_SUCCESS
);
484 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
485 &value_length
, &flags
, &rc
);
486 test_true((value_length
== (sizeof(uint32_t) * x
)));
487 test_true(rc
== MEMCACHED_SUCCESS
);
489 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
491 test_true(*ptr
== store_list
[x
- counter
]);
499 static test_return_t
cas2_test(memcached_st
*memc
)
501 memcached_return_t rc
;
502 const char *keys
[]= {"fudge", "son", "food"};
503 size_t key_length
[]= {5, 3, 4};
504 const char *value
= "we the people";
505 size_t value_length
= strlen("we the people");
507 memcached_result_st results_obj
;
508 memcached_result_st
*results
;
511 rc
= memcached_flush(memc
, 0);
512 test_true(rc
== MEMCACHED_SUCCESS
);
514 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
516 for (x
= 0; x
< 3; x
++)
518 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
519 keys
[x
], key_length
[x
],
520 (time_t)50, (uint32_t)9);
521 test_true(rc
== MEMCACHED_SUCCESS
);
524 rc
= memcached_mget(memc
, keys
, key_length
, 3);
526 results
= memcached_result_create(memc
, &results_obj
);
528 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
530 test_true(results
->item_cas
);
531 test_true(rc
== MEMCACHED_SUCCESS
);
532 test_true(memcached_result_cas(results
));
534 test_true(!memcmp(value
, "we the people", strlen("we the people")));
535 test_true(strlen("we the people") == value_length
);
536 test_true(rc
== MEMCACHED_SUCCESS
);
538 memcached_result_free(&results_obj
);
543 static test_return_t
cas_test(memcached_st
*memc
)
545 memcached_return_t rc
;
546 const char *key
= "fun";
547 size_t key_length
= strlen(key
);
548 const char *value
= "we the people";
549 const char* keys
[2] = { key
, NULL
};
550 size_t keylengths
[2] = { strlen(key
), 0 };
551 size_t value_length
= strlen(value
);
552 const char *value2
= "change the value";
553 size_t value2_length
= strlen(value2
);
555 memcached_result_st results_obj
;
556 memcached_result_st
*results
;
559 rc
= memcached_flush(memc
, 0);
560 test_true(rc
== MEMCACHED_SUCCESS
);
562 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
564 rc
= memcached_set(memc
, key
, strlen(key
),
565 value
, strlen(value
),
566 (time_t)0, (uint32_t)0);
567 test_true(rc
== MEMCACHED_SUCCESS
);
569 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
571 results
= memcached_result_create(memc
, &results_obj
);
573 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
575 test_true(rc
== MEMCACHED_SUCCESS
);
576 test_true(memcached_result_cas(results
));
577 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
578 test_true(strlen(memcached_result_value(results
)) == value_length
);
579 test_true(rc
== MEMCACHED_SUCCESS
);
580 uint64_t cas
= memcached_result_cas(results
);
583 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
584 test_true(rc
== MEMCACHED_END
);
585 test_true(results
== NULL
);
588 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
589 test_true(rc
== MEMCACHED_SUCCESS
);
592 * The item will have a new cas value, so try to set it again with the old
593 * value. This should fail!
595 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
596 test_true(rc
== MEMCACHED_DATA_EXISTS
);
598 memcached_result_free(&results_obj
);
603 static test_return_t
prepend_test(memcached_st
*memc
)
605 memcached_return_t rc
;
606 const char *key
= "fig";
607 const char *value
= "people";
608 char *out_value
= NULL
;
612 rc
= memcached_flush(memc
, 0);
613 test_true(rc
== MEMCACHED_SUCCESS
);
615 rc
= memcached_set(memc
, key
, strlen(key
),
616 value
, strlen(value
),
617 (time_t)0, (uint32_t)0);
618 test_true(rc
== MEMCACHED_SUCCESS
);
620 rc
= memcached_prepend(memc
, key
, strlen(key
),
621 "the ", strlen("the "),
622 (time_t)0, (uint32_t)0);
623 test_true(rc
== MEMCACHED_SUCCESS
);
625 rc
= memcached_prepend(memc
, key
, strlen(key
),
626 "we ", strlen("we "),
627 (time_t)0, (uint32_t)0);
628 test_true(rc
== MEMCACHED_SUCCESS
);
630 out_value
= memcached_get(memc
, key
, strlen(key
),
631 &value_length
, &flags
, &rc
);
632 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
633 test_true(strlen("we the people") == value_length
);
634 test_true(rc
== MEMCACHED_SUCCESS
);
641 Set the value, then quit to make sure it is flushed.
642 Come back in and test that add fails.
644 static test_return_t
add_test(memcached_st
*memc
)
646 memcached_return_t rc
;
647 const char *key
= "foo";
648 const char *value
= "when we sanitize";
649 unsigned long long setting_value
;
651 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
653 rc
= memcached_set(memc
, key
, strlen(key
),
654 value
, strlen(value
),
655 (time_t)0, (uint32_t)0);
656 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
657 memcached_quit(memc
);
658 rc
= memcached_add(memc
, key
, strlen(key
),
659 value
, strlen(value
),
660 (time_t)0, (uint32_t)0);
662 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
665 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
669 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
676 ** There was a problem of leaking filedescriptors in the initial release
677 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
678 ** systems it seems that the kernel is slow on reclaiming the resources
679 ** because the connects starts to time out (the test doesn't do much
680 ** anyway, so just loop 10 iterations)
682 static test_return_t
add_wrapper(memcached_st
*memc
)
684 unsigned int max
= 10000;
692 for (uint32_t x
= 0; x
< max
; x
++)
698 static test_return_t
replace_test(memcached_st
*memc
)
700 memcached_return_t rc
;
701 const char *key
= "foo";
702 const char *value
= "when we sanitize";
703 const char *original
= "first we insert some data";
705 rc
= memcached_set(memc
, key
, strlen(key
),
706 original
, strlen(original
),
707 (time_t)0, (uint32_t)0);
708 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
710 rc
= memcached_replace(memc
, key
, strlen(key
),
711 value
, strlen(value
),
712 (time_t)0, (uint32_t)0);
713 test_true(rc
== MEMCACHED_SUCCESS
);
718 static test_return_t
delete_test(memcached_st
*memc
)
720 memcached_return_t rc
;
721 const char *key
= "foo";
722 const char *value
= "when we sanitize";
724 rc
= memcached_set(memc
, key
, strlen(key
),
725 value
, strlen(value
),
726 (time_t)0, (uint32_t)0);
727 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
729 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
730 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
735 static test_return_t
flush_test(memcached_st
*memc
)
737 memcached_return_t rc
;
739 rc
= memcached_flush(memc
, 0);
740 test_true(rc
== MEMCACHED_SUCCESS
);
745 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
746 const memcached_server_st
*server
__attribute__((unused
)),
747 void *context
__attribute__((unused
)))
751 return MEMCACHED_SUCCESS
;
754 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
757 strcpy(context
, "foo bad");
758 memcached_server_fn callbacks
[1];
760 callbacks
[0]= server_function
;
761 memcached_server_cursor(memc
, callbacks
, context
, 1);
765 static test_return_t
bad_key_test(memcached_st
*memc
)
767 memcached_return_t rc
;
768 const char *key
= "foo bad";
770 size_t string_length
;
772 memcached_st
*memc_clone
;
774 size_t max_keylen
= 0xffff;
776 // Just skip if we are in binary mode.
777 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
780 memc_clone
= memcached_clone(NULL
, memc
);
781 test_true(memc_clone
);
783 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
784 test_true(rc
== MEMCACHED_SUCCESS
);
786 /* All keys are valid in the binary protocol (except for length) */
787 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
789 string
= memcached_get(memc_clone
, key
, strlen(key
),
790 &string_length
, &flags
, &rc
);
791 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
792 test_true(string_length
== 0);
796 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
797 test_true(rc
== MEMCACHED_SUCCESS
);
798 string
= memcached_get(memc_clone
, key
, strlen(key
),
799 &string_length
, &flags
, &rc
);
800 test_true(rc
== MEMCACHED_NOTFOUND
);
801 test_true(string_length
== 0);
804 /* Test multi key for bad keys */
805 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
806 size_t key_lengths
[] = { 7, 7, 7 };
808 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
809 test_true(rc
== MEMCACHED_SUCCESS
);
811 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
812 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
814 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
815 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
819 /* The following test should be moved to the end of this function when the
820 memcached server is updated to allow max size length of the keys in the
823 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
824 test_true(rc
== MEMCACHED_SUCCESS
);
826 char *longkey
= malloc(max_keylen
+ 1);
829 memset(longkey
, 'a', max_keylen
+ 1);
830 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
831 &string_length
, &flags
, &rc
);
832 test_true(rc
== MEMCACHED_NOTFOUND
);
833 test_true(string_length
== 0);
836 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
837 &string_length
, &flags
, &rc
);
838 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
839 test_true(string_length
== 0);
846 /* Make sure zero length keys are marked as bad */
848 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
849 test_true(rc
== MEMCACHED_SUCCESS
);
850 string
= memcached_get(memc_clone
, key
, 0,
851 &string_length
, &flags
, &rc
);
852 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
853 test_true(string_length
== 0);
856 memcached_free(memc_clone
);
861 #define READ_THROUGH_VALUE "set for me"
862 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
863 char *key
__attribute__((unused
)),
864 size_t key_length
__attribute__((unused
)),
865 memcached_result_st
*result
)
868 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
871 static test_return_t
read_through(memcached_st
*memc
)
873 memcached_return_t rc
;
874 const char *key
= "foo";
876 size_t string_length
;
878 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
880 string
= memcached_get(memc
, key
, strlen(key
),
881 &string_length
, &flags
, &rc
);
883 test_true(rc
== MEMCACHED_NOTFOUND
);
884 test_false(string_length
);
887 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
889 test_true(rc
== MEMCACHED_SUCCESS
);
891 string
= memcached_get(memc
, key
, strlen(key
),
892 &string_length
, &flags
, &rc
);
894 test_true(rc
== MEMCACHED_SUCCESS
);
895 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
896 test_strcmp(READ_THROUGH_VALUE
, string
);
899 string
= memcached_get(memc
, key
, strlen(key
),
900 &string_length
, &flags
, &rc
);
902 test_true(rc
== MEMCACHED_SUCCESS
);
903 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
904 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
910 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
912 size_t key_length
__attribute__((unused
)))
916 return MEMCACHED_SUCCESS
;
919 static test_return_t
delete_through(memcached_st
*memc
)
921 memcached_trigger_delete_key_fn callback
;
922 memcached_return_t rc
;
924 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
926 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
927 test_true(rc
== MEMCACHED_SUCCESS
);
932 static test_return_t
get_test(memcached_st
*memc
)
934 memcached_return_t rc
;
935 const char *key
= "foo";
937 size_t string_length
;
940 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
941 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
943 string
= memcached_get(memc
, key
, strlen(key
),
944 &string_length
, &flags
, &rc
);
946 test_true(rc
== MEMCACHED_NOTFOUND
);
947 test_false(string_length
);
953 static test_return_t
get_test2(memcached_st
*memc
)
955 memcached_return_t rc
;
956 const char *key
= "foo";
957 const char *value
= "when we sanitize";
959 size_t string_length
;
962 rc
= memcached_set(memc
, key
, strlen(key
),
963 value
, strlen(value
),
964 (time_t)0, (uint32_t)0);
965 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
967 string
= memcached_get(memc
, key
, strlen(key
),
968 &string_length
, &flags
, &rc
);
971 test_true(rc
== MEMCACHED_SUCCESS
);
972 test_true(string_length
== strlen(value
));
973 test_true(!memcmp(string
, value
, string_length
));
980 static test_return_t
set_test2(memcached_st
*memc
)
982 memcached_return_t rc
;
983 const char *key
= "foo";
984 const char *value
= "train in the brain";
985 size_t value_length
= strlen(value
);
988 for (x
= 0; x
< 10; x
++)
990 rc
= memcached_set(memc
, key
, strlen(key
),
992 (time_t)0, (uint32_t)0);
993 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
999 static test_return_t
set_test3(memcached_st
*memc
)
1001 memcached_return_t rc
;
1003 size_t value_length
= 8191;
1006 value
= (char*)malloc(value_length
);
1009 for (x
= 0; x
< value_length
; x
++)
1010 value
[x
] = (char) (x
% 127);
1012 /* The dump test relies on there being at least 32 items in memcached */
1013 for (x
= 0; x
< 32; x
++)
1017 sprintf(key
, "foo%u", x
);
1019 rc
= memcached_set(memc
, key
, strlen(key
),
1020 value
, value_length
,
1021 (time_t)0, (uint32_t)0);
1022 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1027 return TEST_SUCCESS
;
1030 static test_return_t
get_test3(memcached_st
*memc
)
1032 memcached_return_t rc
;
1033 const char *key
= "foo";
1035 size_t value_length
= 8191;
1037 size_t string_length
;
1041 value
= (char*)malloc(value_length
);
1044 for (x
= 0; x
< value_length
; x
++)
1045 value
[x
] = (char) (x
% 127);
1047 rc
= memcached_set(memc
, key
, strlen(key
),
1048 value
, value_length
,
1049 (time_t)0, (uint32_t)0);
1050 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1052 string
= memcached_get(memc
, key
, strlen(key
),
1053 &string_length
, &flags
, &rc
);
1055 test_true(rc
== MEMCACHED_SUCCESS
);
1057 test_true(string_length
== value_length
);
1058 test_true(!memcmp(string
, value
, string_length
));
1063 return TEST_SUCCESS
;
1066 static test_return_t
get_test4(memcached_st
*memc
)
1068 memcached_return_t rc
;
1069 const char *key
= "foo";
1071 size_t value_length
= 8191;
1073 size_t string_length
;
1077 value
= (char*)malloc(value_length
);
1080 for (x
= 0; x
< value_length
; x
++)
1081 value
[x
] = (char) (x
% 127);
1083 rc
= memcached_set(memc
, key
, strlen(key
),
1084 value
, value_length
,
1085 (time_t)0, (uint32_t)0);
1086 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1088 for (x
= 0; x
< 10; x
++)
1090 string
= memcached_get(memc
, key
, strlen(key
),
1091 &string_length
, &flags
, &rc
);
1093 test_true(rc
== MEMCACHED_SUCCESS
);
1095 test_true(string_length
== value_length
);
1096 test_true(!memcmp(string
, value
, string_length
));
1102 return TEST_SUCCESS
;
1106 * This test verifies that memcached_read_one_response doesn't try to
1107 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1108 * responses before you execute a storage command.
1110 static test_return_t
get_test5(memcached_st
*memc
)
1113 ** Request the same key twice, to ensure that we hash to the same server
1114 ** (so that we have multiple response values queued up) ;-)
1116 const char *keys
[]= { "key", "key" };
1117 size_t lengths
[]= { 3, 3 };
1121 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1122 keys
[0], lengths
[0], 0, 0);
1123 test_true(rc
== MEMCACHED_SUCCESS
);
1124 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1126 memcached_result_st results_obj
;
1127 memcached_result_st
*results
;
1128 results
=memcached_result_create(memc
, &results_obj
);
1130 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1132 memcached_result_free(&results_obj
);
1134 /* Don't read out the second result, but issue a set instead.. */
1135 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1136 test_true(rc
== MEMCACHED_SUCCESS
);
1138 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1139 &rlen
, &flags
, &rc
);
1140 test_true(val
== NULL
);
1141 test_true(rc
== MEMCACHED_NOTFOUND
);
1142 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1143 test_true(val
!= NULL
);
1144 test_true(rc
== MEMCACHED_SUCCESS
);
1147 return TEST_SUCCESS
;
1150 static test_return_t
mget_end(memcached_st
*memc
)
1152 const char *keys
[]= { "foo", "foo2" };
1153 size_t lengths
[]= { 3, 4 };
1154 const char *values
[]= { "fjord", "41" };
1156 memcached_return_t rc
;
1159 for (int i
= 0; i
< 2; i
++)
1161 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1162 (time_t)0, (uint32_t)0);
1163 test_true(rc
== MEMCACHED_SUCCESS
);
1167 size_t string_length
;
1170 // retrieve both via mget
1171 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1172 test_true(rc
== MEMCACHED_SUCCESS
);
1174 char key
[MEMCACHED_MAX_KEY
];
1177 // this should get both
1178 for (int i
= 0; i
< 2; i
++)
1180 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1182 test_true(rc
== MEMCACHED_SUCCESS
);
1184 if (key_length
== 4)
1186 test_true(string_length
== strlen(values
[val
]));
1187 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1191 // this should indicate end
1192 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1193 test_true(rc
== MEMCACHED_END
);
1196 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1197 test_true(rc
== MEMCACHED_SUCCESS
);
1199 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1200 test_true(key_length
== lengths
[0]);
1201 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1202 test_true(string_length
== strlen(values
[0]));
1203 test_true(strncmp(values
[0], string
, string_length
) == 0);
1204 test_true(rc
== MEMCACHED_SUCCESS
);
1207 // this should indicate end
1208 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1209 test_true(rc
== MEMCACHED_END
);
1211 return TEST_SUCCESS
;
1214 /* Do not copy the style of this code, I just access hosts to testthis function */
1215 static test_return_t
stats_servername_test(memcached_st
*memc
)
1217 memcached_return_t rc
;
1218 memcached_stat_st memc_stat
;
1219 memcached_server_instance_st instance
=
1220 memcached_server_instance_by_position(memc
, 0);
1222 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1223 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1224 return TEST_SKIPPED
;
1226 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1227 memcached_server_name(instance
),
1228 memcached_server_port(instance
));
1230 return TEST_SUCCESS
;
1233 static test_return_t
increment_test(memcached_st
*memc
)
1235 uint64_t new_number
;
1236 memcached_return_t rc
;
1237 const char *key
= "number";
1238 const char *value
= "0";
1240 rc
= memcached_set(memc
, key
, strlen(key
),
1241 value
, strlen(value
),
1242 (time_t)0, (uint32_t)0);
1243 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1245 rc
= memcached_increment(memc
, key
, strlen(key
),
1247 test_true(rc
== MEMCACHED_SUCCESS
);
1248 test_true(new_number
== 1);
1250 rc
= memcached_increment(memc
, key
, strlen(key
),
1252 test_true(rc
== MEMCACHED_SUCCESS
);
1253 test_true(new_number
== 2);
1255 return TEST_SUCCESS
;
1258 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1260 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1262 uint64_t new_number
;
1263 memcached_return_t rc
;
1264 const char *key
= "number";
1265 uint64_t initial
= 0;
1267 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1268 1, initial
, 0, &new_number
);
1269 test_true(rc
== MEMCACHED_SUCCESS
);
1270 test_true(new_number
== initial
);
1272 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1273 1, initial
, 0, &new_number
);
1274 test_true(rc
== MEMCACHED_SUCCESS
);
1275 test_true(new_number
== (initial
+ 1));
1277 return TEST_SUCCESS
;
1280 static test_return_t
decrement_test(memcached_st
*memc
)
1282 uint64_t new_number
;
1283 memcached_return_t rc
;
1284 const char *key
= "number";
1285 const char *value
= "3";
1287 rc
= memcached_set(memc
, key
, strlen(key
),
1288 value
, strlen(value
),
1289 (time_t)0, (uint32_t)0);
1290 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1292 rc
= memcached_decrement(memc
, key
, strlen(key
),
1294 test_true(rc
== MEMCACHED_SUCCESS
);
1295 test_true(new_number
== 2);
1297 rc
= memcached_decrement(memc
, key
, strlen(key
),
1299 test_true(rc
== MEMCACHED_SUCCESS
);
1300 test_true(new_number
== 1);
1302 return TEST_SUCCESS
;
1305 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1307 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1309 uint64_t new_number
;
1310 memcached_return_t rc
;
1311 const char *key
= "number";
1312 uint64_t initial
= 3;
1314 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1315 1, initial
, 0, &new_number
);
1316 test_true(rc
== MEMCACHED_SUCCESS
);
1317 test_true(new_number
== initial
);
1319 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1320 1, initial
, 0, &new_number
);
1321 test_true(rc
== MEMCACHED_SUCCESS
);
1322 test_true(new_number
== (initial
- 1));
1324 return TEST_SUCCESS
;
1327 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1329 uint64_t new_number
;
1330 memcached_return_t rc
;
1331 const char *master_key
= "foo";
1332 const char *key
= "number";
1333 const char *value
= "0";
1335 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1337 value
, strlen(value
),
1338 (time_t)0, (uint32_t)0);
1339 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1341 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1343 test_true(rc
== MEMCACHED_SUCCESS
);
1344 test_true(new_number
== 1);
1346 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1348 test_true(rc
== MEMCACHED_SUCCESS
);
1349 test_true(new_number
== 2);
1351 return TEST_SUCCESS
;
1354 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1356 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1358 uint64_t new_number
;
1359 memcached_return_t rc
;
1360 const char *master_key
= "foo";
1361 const char *key
= "number";
1362 uint64_t initial
= 0;
1364 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1366 1, initial
, 0, &new_number
);
1367 test_true(rc
== MEMCACHED_SUCCESS
);
1368 test_true(new_number
== initial
);
1370 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1372 1, initial
, 0, &new_number
);
1373 test_true(rc
== MEMCACHED_SUCCESS
);
1374 test_true(new_number
== (initial
+ 1));
1376 return TEST_SUCCESS
;
1379 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1381 uint64_t new_number
;
1382 memcached_return_t rc
;
1383 const char *master_key
= "foo";
1384 const char *key
= "number";
1385 const char *value
= "3";
1387 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1389 value
, strlen(value
),
1390 (time_t)0, (uint32_t)0);
1391 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1393 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1396 test_true(rc
== MEMCACHED_SUCCESS
);
1397 test_true(new_number
== 2);
1399 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1402 test_true(rc
== MEMCACHED_SUCCESS
);
1403 test_true(new_number
== 1);
1405 return TEST_SUCCESS
;
1408 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1410 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1412 uint64_t new_number
;
1413 memcached_return_t rc
;
1414 const char *master_key
= "foo";
1415 const char *key
= "number";
1416 uint64_t initial
= 3;
1418 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1420 1, initial
, 0, &new_number
);
1421 test_true(rc
== MEMCACHED_SUCCESS
);
1422 test_true(new_number
== initial
);
1424 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1426 1, initial
, 0, &new_number
);
1427 test_true(rc
== MEMCACHED_SUCCESS
);
1428 test_true(new_number
== (initial
- 1));
1430 return TEST_SUCCESS
;
1433 static test_return_t
quit_test(memcached_st
*memc
)
1435 memcached_return_t rc
;
1436 const char *key
= "fudge";
1437 const char *value
= "sanford and sun";
1439 rc
= memcached_set(memc
, key
, strlen(key
),
1440 value
, strlen(value
),
1441 (time_t)10, (uint32_t)3);
1442 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1443 memcached_quit(memc
);
1445 rc
= memcached_set(memc
, key
, strlen(key
),
1446 value
, strlen(value
),
1447 (time_t)50, (uint32_t)9);
1448 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1450 return TEST_SUCCESS
;
1453 static test_return_t
mget_result_test(memcached_st
*memc
)
1455 memcached_return_t rc
;
1456 const char *keys
[]= {"fudge", "son", "food"};
1457 size_t key_length
[]= {5, 3, 4};
1460 memcached_result_st results_obj
;
1461 memcached_result_st
*results
;
1463 results
= memcached_result_create(memc
, &results_obj
);
1465 test_true(&results_obj
== results
);
1467 /* We need to empty the server before continueing test */
1468 rc
= memcached_flush(memc
, 0);
1469 test_true(rc
== MEMCACHED_SUCCESS
);
1471 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1472 test_true(rc
== MEMCACHED_SUCCESS
);
1474 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1479 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1480 test_true(!results
);
1481 test_true(rc
== MEMCACHED_END
);
1483 for (x
= 0; x
< 3; x
++)
1485 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1486 keys
[x
], key_length
[x
],
1487 (time_t)50, (uint32_t)9);
1488 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1491 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1492 test_true(rc
== MEMCACHED_SUCCESS
);
1494 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1497 test_true(&results_obj
== results
);
1498 test_true(rc
== MEMCACHED_SUCCESS
);
1499 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1500 test_true(!memcmp(memcached_result_key_value(results
),
1501 memcached_result_value(results
),
1502 memcached_result_length(results
)));
1505 memcached_result_free(&results_obj
);
1507 return TEST_SUCCESS
;
1510 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1512 memcached_return_t rc
;
1513 const char *keys
[]= {"fudge", "son", "food"};
1514 size_t key_length
[]= {5, 3, 4};
1517 memcached_result_st
*results
;
1519 /* We need to empty the server before continueing test */
1520 rc
= memcached_flush(memc
, 0);
1521 test_true(rc
== MEMCACHED_SUCCESS
);
1523 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1524 test_true(rc
== MEMCACHED_SUCCESS
);
1526 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1530 test_true(!results
);
1531 test_true(rc
== MEMCACHED_END
);
1533 for (x
= 0; x
< 3; x
++)
1535 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1536 keys
[x
], key_length
[x
],
1537 (time_t)50, (uint32_t)9);
1538 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1541 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1542 test_true(rc
== MEMCACHED_SUCCESS
);
1545 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1548 test_true(rc
== MEMCACHED_SUCCESS
);
1549 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1550 test_true(!memcmp(memcached_result_key_value(results
),
1551 memcached_result_value(results
),
1552 memcached_result_length(results
)));
1553 memcached_result_free(results
);
1557 return TEST_SUCCESS
;
1560 /* Count the results */
1561 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1562 memcached_result_st
*result
__attribute__((unused
)),
1565 size_t *counter
= (size_t *)context
;
1567 *counter
= *counter
+ 1;
1569 return MEMCACHED_SUCCESS
;
1572 static test_return_t
mget_result_function(memcached_st
*memc
)
1574 memcached_return_t rc
;
1575 const char *keys
[]= {"fudge", "son", "food"};
1576 size_t key_length
[]= {5, 3, 4};
1579 memcached_execute_fn callbacks
[1];
1581 /* We need to empty the server before continueing test */
1582 rc
= memcached_flush(memc
, 0);
1583 for (x
= 0; x
< 3; x
++)
1585 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1586 keys
[x
], key_length
[x
],
1587 (time_t)50, (uint32_t)9);
1588 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1591 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1592 test_true(rc
== MEMCACHED_SUCCESS
);
1594 callbacks
[0]= &callback_counter
;
1596 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1598 test_true(counter
== 3);
1600 return TEST_SUCCESS
;
1603 static test_return_t
mget_test(memcached_st
*memc
)
1605 memcached_return_t rc
;
1606 const char *keys
[]= {"fudge", "son", "food"};
1607 size_t key_length
[]= {5, 3, 4};
1611 char return_key
[MEMCACHED_MAX_KEY
];
1612 size_t return_key_length
;
1614 size_t return_value_length
;
1616 /* We need to empty the server before continueing test */
1617 rc
= memcached_flush(memc
, 0);
1618 test_true(rc
== MEMCACHED_SUCCESS
);
1620 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1621 test_true(rc
== MEMCACHED_SUCCESS
);
1623 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1624 &return_value_length
, &flags
, &rc
)) != NULL
)
1626 test_true(return_value
);
1628 test_true(!return_value
);
1629 test_true(return_value_length
== 0);
1630 test_true(rc
== MEMCACHED_END
);
1632 for (x
= 0; x
< 3; x
++)
1634 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1635 keys
[x
], key_length
[x
],
1636 (time_t)50, (uint32_t)9);
1637 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1640 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1641 test_true(rc
== MEMCACHED_SUCCESS
);
1644 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1645 &return_value_length
, &flags
, &rc
)))
1647 test_true(return_value
);
1648 test_true(rc
== MEMCACHED_SUCCESS
);
1649 test_true(return_key_length
== return_value_length
);
1650 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1655 return TEST_SUCCESS
;
1658 static test_return_t
mget_execute(memcached_st
*memc
)
1662 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1666 * I only want to hit _one_ server so I know the number of requests I'm
1667 * sending in the pipeline.
1669 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1670 memc
->number_of_hosts
= 1;
1672 size_t max_keys
= 20480;
1675 char **keys
= calloc(max_keys
, sizeof(char*));
1676 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1678 /* First add all of the items.. */
1679 char blob
[1024] = {0};
1680 memcached_return_t rc
;
1682 for (size_t x
= 0; x
< max_keys
; ++x
)
1686 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
1688 test_true(keys
[x
] != NULL
);
1689 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1690 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1693 /* Try to get all of them with a large multiget */
1695 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1696 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1697 max_keys
, callbacks
, &counter
, 1);
1699 if (rc
== MEMCACHED_SUCCESS
)
1702 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1703 test_true(rc
== MEMCACHED_END
);
1705 /* Verify that we got all of the items */
1706 test_true(counter
== max_keys
);
1708 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1710 test_true(counter
== 0);
1714 test_fail("note: this test functions differently when in binary mode");
1717 /* Release all allocated resources */
1718 for (size_t x
= 0; x
< max_keys
; ++x
)
1725 memc
->number_of_hosts
= number_of_hosts
;
1726 return TEST_SUCCESS
;
1729 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1731 static test_return_t
key_setup(memcached_st
*memc
)
1735 if (pre_binary(memc
) != TEST_SUCCESS
)
1736 return TEST_SKIPPED
;
1738 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1740 return TEST_SUCCESS
;
1743 static test_return_t
key_teardown(memcached_st
*memc
)
1746 pairs_free(global_pairs
);
1748 return TEST_SUCCESS
;
1751 static test_return_t
block_add_regression(memcached_st
*memc
)
1753 /* First add all of the items.. */
1754 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1756 memcached_return_t rc
;
1757 char blob
[1024] = {0};
1759 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1760 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1763 return TEST_SUCCESS
;
1766 static test_return_t
binary_add_regression(memcached_st
*memc
)
1768 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1769 test_return_t rc
= block_add_regression(memc
);
1770 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1774 static test_return_t
get_stats_keys(memcached_st
*memc
)
1778 memcached_stat_st memc_stat
;
1779 memcached_return_t rc
;
1781 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1782 test_true(rc
== MEMCACHED_SUCCESS
);
1783 for (ptr
= stat_list
; *ptr
; ptr
++)
1788 return TEST_SUCCESS
;
1791 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1793 const char *version_string
;
1795 version_string
= memcached_lib_version();
1797 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1799 return TEST_SUCCESS
;
1802 static test_return_t
get_stats(memcached_st
*memc
)
1806 memcached_return_t rc
;
1807 memcached_stat_st
*memc_stat
;
1809 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1810 test_true(rc
== MEMCACHED_SUCCESS
);
1812 test_true(rc
== MEMCACHED_SUCCESS
);
1813 test_true(memc_stat
);
1815 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1817 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1818 test_true(rc
== MEMCACHED_SUCCESS
);
1819 for (ptr
= stat_list
; *ptr
; ptr
++);
1824 memcached_stat_free(NULL
, memc_stat
);
1826 return TEST_SUCCESS
;
1829 static test_return_t
add_host_test(memcached_st
*memc
)
1832 memcached_server_st
*servers
;
1833 memcached_return_t rc
;
1834 char servername
[]= "0.example.com";
1836 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1838 test_true(1 == memcached_server_list_count(servers
));
1840 for (x
= 2; x
< 20; x
++)
1842 char buffer
[SMALL_STRING_LEN
];
1844 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1845 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1847 test_true(rc
== MEMCACHED_SUCCESS
);
1848 test_true(x
== memcached_server_list_count(servers
));
1851 rc
= memcached_server_push(memc
, servers
);
1852 test_true(rc
== MEMCACHED_SUCCESS
);
1853 rc
= memcached_server_push(memc
, servers
);
1854 test_true(rc
== MEMCACHED_SUCCESS
);
1856 memcached_server_list_free(servers
);
1858 return TEST_SUCCESS
;
1861 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1863 return MEMCACHED_SUCCESS
;
1866 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1868 return MEMCACHED_SUCCESS
;
1871 static test_return_t
callback_test(memcached_st
*memc
)
1873 /* Test User Data */
1877 memcached_return_t rc
;
1879 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1880 test_true(rc
== MEMCACHED_SUCCESS
);
1881 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1882 test_true(*test_ptr
== x
);
1885 /* Test Clone Callback */
1887 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1888 void *clone_cb_ptr
= *(void **)&clone_cb
;
1889 void *temp_function
= NULL
;
1890 memcached_return_t rc
;
1892 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1894 test_true(rc
== MEMCACHED_SUCCESS
);
1895 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1896 test_true(temp_function
== clone_cb_ptr
);
1899 /* Test Cleanup Callback */
1901 memcached_cleanup_fn cleanup_cb
=
1902 (memcached_cleanup_fn
)cleanup_test_callback
;
1903 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1904 void *temp_function
= NULL
;
1905 memcached_return_t rc
;
1907 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1909 test_true(rc
== MEMCACHED_SUCCESS
);
1910 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1911 test_true(temp_function
== cleanup_cb_ptr
);
1914 return TEST_SUCCESS
;
1917 /* We don't test the behavior itself, we test the switches */
1918 static test_return_t
behavior_test(memcached_st
*memc
)
1923 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1924 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1925 test_true(value
== 1);
1927 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1928 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1929 test_true(value
== 1);
1931 set
= MEMCACHED_HASH_MD5
;
1932 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1933 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1934 test_true(value
== MEMCACHED_HASH_MD5
);
1938 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1939 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1940 test_true(value
== 0);
1942 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1943 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1944 test_true(value
== 0);
1946 set
= MEMCACHED_HASH_DEFAULT
;
1947 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1948 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1949 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1951 set
= MEMCACHED_HASH_CRC
;
1952 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1953 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1954 test_true(value
== MEMCACHED_HASH_CRC
);
1956 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1957 test_true(value
> 0);
1959 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1960 test_true(value
> 0);
1962 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1963 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1964 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1966 return TEST_SUCCESS
;
1969 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1971 memcached_return_t rc
;
1975 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1976 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1978 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1980 if (rc
== MEMCACHED_SUCCESS
)
1982 test_true((bool)value
== set
);
1986 test_false((bool)value
== set
);
1989 return TEST_SUCCESS
;
1993 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
1995 memcached_return_t rc
;
1999 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2000 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2002 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2004 if (rc
== MEMCACHED_SUCCESS
)
2006 test_true((bool)value
== set
);
2010 test_false((bool)value
== set
);
2013 return TEST_SUCCESS
;
2017 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2019 memcached_return_t rc
;
2023 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2024 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2026 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2028 if (rc
== MEMCACHED_SUCCESS
)
2030 test_true((bool)value
== set
);
2034 test_false((bool)value
== set
);
2037 return TEST_SUCCESS
;
2040 static test_return_t
fetch_all_results(memcached_st
*memc
)
2042 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2043 char return_key
[MEMCACHED_MAX_KEY
];
2044 size_t return_key_length
;
2046 size_t return_value_length
;
2049 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2050 &return_value_length
, &flags
, &rc
)))
2052 test_true(return_value
);
2053 test_true(rc
== MEMCACHED_SUCCESS
);
2057 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
2060 /* Test case provided by Cal Haldenbrand */
2061 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2063 unsigned int setter
= 1;
2065 unsigned long long total
= 0;
2068 char randomstuff
[6 * 1024];
2069 memcached_return_t rc
;
2071 memset(randomstuff
, 0, 6 * 1024);
2073 /* We just keep looking at the same values over and over */
2076 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2077 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2081 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2085 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2086 memset(randomstuff
, 0, 6 * 1024);
2087 test_true(size
< 6 * 1024); /* Being safe here */
2089 for (j
= 0 ; j
< size
;j
++)
2090 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2093 snprintf(key
, sizeof(key
), "%u", x
);
2094 rc
= memcached_set(memc
, key
, strlen(key
),
2095 randomstuff
, strlen(randomstuff
), 10, 0);
2096 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2097 /* If we fail, lets try again */
2098 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2099 rc
= memcached_set(memc
, key
, strlen(key
),
2100 randomstuff
, strlen(randomstuff
), 10, 0);
2101 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2104 return TEST_SUCCESS
;
2107 /* Test case provided by Cal Haldenbrand */
2108 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2110 unsigned int setter
;
2114 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2115 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2117 setter
= 20 * 1024576;
2118 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2119 setter
= 20 * 1024576;
2120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2121 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2122 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2124 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2127 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2129 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2130 char buffer
[SMALL_STRING_LEN
];
2135 memset(buffer
, 0, SMALL_STRING_LEN
);
2137 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2138 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2139 &val_len
, &flags
, &rc
);
2140 if (rc
!= MEMCACHED_SUCCESS
)
2142 if (rc
== MEMCACHED_NOTFOUND
)
2156 return TEST_SUCCESS
;
2159 /* Do a large mget() over all the keys we think exist */
2160 #define KEY_COUNT 3000 // * 1024576
2161 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2163 memcached_return_t rc
;
2164 unsigned int setter
;
2167 size_t key_lengths
[KEY_COUNT
];
2170 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2171 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2173 setter
= 20 * 1024576;
2174 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2175 setter
= 20 * 1024576;
2176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2177 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2178 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2181 keys
= calloc(KEY_COUNT
, sizeof(char *));
2183 for (x
= 0; x
< KEY_COUNT
; x
++)
2187 snprintf(buffer
, 30, "%u", x
);
2188 keys
[x
]= strdup(buffer
);
2189 key_lengths
[x
]= strlen(keys
[x
]);
2192 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2193 test_true(rc
== MEMCACHED_SUCCESS
);
2195 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2197 for (x
= 0; x
< KEY_COUNT
; x
++)
2201 return TEST_SUCCESS
;
2204 /* Make sure we behave properly if server list has no values */
2205 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2207 memcached_return_t rc
;
2208 const char *keys
[]= {"fudge", "son", "food"};
2209 size_t key_length
[]= {5, 3, 4};
2212 char return_key
[MEMCACHED_MAX_KEY
];
2213 size_t return_key_length
;
2215 size_t return_value_length
;
2217 /* Here we free everything before running a bunch of mget tests */
2218 memcached_servers_reset(memc
);
2221 /* We need to empty the server before continueing test */
2222 rc
= memcached_flush(memc
, 0);
2223 test_true(rc
== MEMCACHED_NO_SERVERS
);
2225 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2226 test_true(rc
== MEMCACHED_NO_SERVERS
);
2228 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2229 &return_value_length
, &flags
, &rc
)) != NULL
)
2231 test_true(return_value
);
2233 test_true(!return_value
);
2234 test_true(return_value_length
== 0);
2235 test_true(rc
== MEMCACHED_NO_SERVERS
);
2237 for (x
= 0; x
< 3; x
++)
2239 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2240 keys
[x
], key_length
[x
],
2241 (time_t)50, (uint32_t)9);
2242 test_true(rc
== MEMCACHED_NO_SERVERS
);
2245 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2246 test_true(rc
== MEMCACHED_NO_SERVERS
);
2249 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2250 &return_value_length
, &flags
, &rc
)))
2252 test_true(return_value
);
2253 test_true(rc
== MEMCACHED_SUCCESS
);
2254 test_true(return_key_length
== return_value_length
);
2255 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2260 return TEST_SUCCESS
;
2263 #define VALUE_SIZE_BUG5 1048064
2264 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2266 memcached_return_t rc
;
2267 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2268 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2269 char return_key
[MEMCACHED_MAX_KEY
];
2270 size_t return_key_length
;
2272 size_t value_length
;
2276 char insert_data
[VALUE_SIZE_BUG5
];
2278 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2279 insert_data
[x
]= (signed char)rand();
2281 memcached_flush(memc
, 0);
2282 value
= memcached_get(memc
, keys
[0], key_length
[0],
2283 &value_length
, &flags
, &rc
);
2284 test_true(value
== NULL
);
2285 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2288 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2289 &value_length
, &flags
, &rc
)))
2291 test_true(count
== 0);
2293 for (x
= 0; x
< 4; x
++)
2295 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2296 insert_data
, VALUE_SIZE_BUG5
,
2297 (time_t)0, (uint32_t)0);
2298 test_true(rc
== MEMCACHED_SUCCESS
);
2301 for (x
= 0; x
< 10; x
++)
2303 value
= memcached_get(memc
, keys
[0], key_length
[0],
2304 &value_length
, &flags
, &rc
);
2308 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2310 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2311 &value_length
, &flags
, &rc
)))
2316 test_true(count
== 4);
2319 return TEST_SUCCESS
;
2322 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2324 memcached_return_t rc
;
2325 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2326 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2327 char return_key
[MEMCACHED_MAX_KEY
];
2328 size_t return_key_length
;
2330 size_t value_length
;
2334 char insert_data
[VALUE_SIZE_BUG5
];
2336 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2337 insert_data
[x
]= (signed char)rand();
2339 memcached_flush(memc
, 0);
2340 value
= memcached_get(memc
, keys
[0], key_length
[0],
2341 &value_length
, &flags
, &rc
);
2342 test_true(value
== NULL
);
2343 test_true(rc
== MEMCACHED_NOTFOUND
);
2344 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2345 test_true(rc
== MEMCACHED_SUCCESS
);
2348 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2349 &value_length
, &flags
, &rc
)))
2351 test_true(count
== 0);
2352 test_true(rc
== MEMCACHED_END
);
2354 for (x
= 0; x
< 4; x
++)
2356 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2357 insert_data
, VALUE_SIZE_BUG5
,
2358 (time_t)0, (uint32_t)0);
2359 test_true(rc
== MEMCACHED_SUCCESS
);
2362 for (x
= 0; x
< 2; x
++)
2364 value
= memcached_get(memc
, keys
[0], key_length
[0],
2365 &value_length
, &flags
, &rc
);
2369 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2370 test_true(rc
== MEMCACHED_SUCCESS
);
2372 /* We test for purge of partial complete fetches */
2373 for (count
= 3; count
; count
--)
2375 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2376 &value_length
, &flags
, &rc
);
2377 test_true(rc
== MEMCACHED_SUCCESS
);
2378 test_true(!(memcmp(value
, insert_data
, value_length
)));
2379 test_true(value_length
);
2384 return TEST_SUCCESS
;
2387 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2389 memcached_return_t rc
;
2391 memcached_st
*memc_clone
;
2393 memcached_server_st
*servers
;
2394 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";
2396 servers
= memcached_servers_parse(server_list
);
2399 mine
= memcached_create(NULL
);
2400 rc
= memcached_server_push(mine
, servers
);
2401 test_true(rc
== MEMCACHED_SUCCESS
);
2402 memcached_server_list_free(servers
);
2405 memc_clone
= memcached_clone(NULL
, mine
);
2407 memcached_quit(mine
);
2408 memcached_quit(memc_clone
);
2411 memcached_free(mine
);
2412 memcached_free(memc_clone
);
2414 return TEST_SUCCESS
;
2417 /* Test flag store/retrieve */
2418 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2420 memcached_return_t rc
;
2421 const char *keys
= "036790384900";
2422 size_t key_length
= strlen(keys
);
2423 char return_key
[MEMCACHED_MAX_KEY
];
2424 size_t return_key_length
;
2426 size_t value_length
;
2429 char insert_data
[VALUE_SIZE_BUG5
];
2431 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2432 insert_data
[x
]= (signed char)rand();
2434 memcached_flush(memc
, 0);
2437 rc
= memcached_set(memc
, keys
, key_length
,
2438 insert_data
, VALUE_SIZE_BUG5
,
2440 test_true(rc
== MEMCACHED_SUCCESS
);
2443 value
= memcached_get(memc
, keys
, key_length
,
2444 &value_length
, &flags
, &rc
);
2445 test_true(flags
== 245);
2449 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2452 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2453 &value_length
, &flags
, &rc
);
2454 test_true(flags
== 245);
2459 return TEST_SUCCESS
;
2462 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2464 memcached_return_t rc
;
2465 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2466 size_t key_length
[3];
2471 char return_key
[MEMCACHED_MAX_KEY
];
2472 size_t return_key_length
;
2474 size_t return_value_length
;
2477 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2478 key_length
[1]= strlen("fudge&*@#");
2479 key_length
[2]= strlen("for^#@&$not");
2482 for (x
= 0; x
< 3; x
++)
2484 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2485 keys
[x
], key_length
[x
],
2486 (time_t)50, (uint32_t)9);
2487 test_true(rc
== MEMCACHED_SUCCESS
);
2490 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2491 test_true(rc
== MEMCACHED_SUCCESS
);
2493 /* We need to empty the server before continueing test */
2494 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2495 &return_value_length
, &flags
, &rc
)) != NULL
)
2497 test_true(return_value
);
2501 test_true(count
== 3);
2503 return TEST_SUCCESS
;
2506 /* We are testing with aggressive timeout to get failures */
2507 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2509 const char *key
= "foo";
2511 size_t value_length
= 512;
2514 memcached_return_t rc
;
2515 unsigned int set
= 1;
2516 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2519 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2520 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2522 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2525 value
= (char*)malloc(value_length
* sizeof(char));
2527 for (x
= 0; x
< value_length
; x
++)
2528 value
[x
]= (char) (x
% 127);
2530 for (x
= 1; x
<= 100000; ++x
)
2532 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2534 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2535 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2537 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2542 memcached_free(mclone
);
2544 return TEST_SUCCESS
;
2548 We are looking failures in the async protocol
2550 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2552 const char *key
= "foo";
2554 size_t value_length
= 512;
2557 memcached_return_t rc
;
2558 unsigned int set
= 1;
2560 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2562 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2563 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2565 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2568 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2570 test_true(timeout
== -1);
2572 value
= (char*)malloc(value_length
* sizeof(char));
2574 for (x
= 0; x
< value_length
; x
++)
2575 value
[x
]= (char) (x
% 127);
2577 for (x
= 1; x
<= 100000; ++x
)
2579 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2583 memcached_free(mclone
);
2585 return TEST_SUCCESS
;
2589 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2591 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2593 memcached_return_t rc
;
2595 size_t value_length
;
2597 uint64_t number_value
;
2599 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2600 &value_length
, &flags
, &rc
);
2601 test_true(value
== NULL
);
2602 test_true(rc
== MEMCACHED_NOTFOUND
);
2604 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2607 test_true(value
== NULL
);
2608 /* The binary protocol will set the key if it doesn't exist */
2609 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2611 test_true(rc
== MEMCACHED_SUCCESS
);
2615 test_true(rc
== MEMCACHED_NOTFOUND
);
2618 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2620 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2621 &value_length
, &flags
, &rc
);
2623 test_true(rc
== MEMCACHED_SUCCESS
);
2626 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2628 test_true(number_value
== 2);
2629 test_true(rc
== MEMCACHED_SUCCESS
);
2631 return TEST_SUCCESS
;
2635 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2636 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2638 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2640 char key
[] = "key34567890";
2642 memcached_return_t rc
;
2643 size_t overflowSize
;
2645 char commandFirst
[]= "set key34567890 0 0 ";
2646 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2647 size_t commandLength
;
2650 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2652 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2654 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2656 overflow
= malloc(testSize
);
2657 test_true(overflow
!= NULL
);
2659 memset(overflow
, 'x', testSize
);
2660 rc
= memcached_set(memc
, key
, strlen(key
),
2661 overflow
, testSize
, 0, 0);
2662 test_true(rc
== MEMCACHED_SUCCESS
);
2666 return TEST_SUCCESS
;
2671 Test values of many different sizes
2672 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2673 set key34567890 0 0 8169 \r\n
2674 is sent followed by buffer of size 8169, followed by 8169
2676 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2679 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2680 memcached_return_t rc
;
2681 const char *key
= "foo";
2683 size_t value_length
= 18000;
2685 size_t string_length
;
2688 size_t current_length
;
2690 value
= (char*)malloc(value_length
);
2693 for (x
= 0; x
< value_length
; x
++)
2694 value
[x
] = (char) (x
% 127);
2696 for (current_length
= 0; current_length
< value_length
; current_length
++)
2698 rc
= memcached_set(memc
, key
, strlen(key
),
2699 value
, current_length
,
2700 (time_t)0, (uint32_t)0);
2701 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2703 string
= memcached_get(memc
, key
, strlen(key
),
2704 &string_length
, &flags
, &rc
);
2706 test_true(rc
== MEMCACHED_SUCCESS
);
2707 test_true(string_length
== current_length
);
2708 test_true(!memcmp(string
, value
, string_length
));
2715 return TEST_SUCCESS
;
2719 Look for zero length value problems
2721 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2724 memcached_return_t rc
;
2725 const char *key
= "mykey";
2730 for (x
= 0; x
< 2; x
++)
2732 rc
= memcached_set(memc
, key
, strlen(key
),
2734 (time_t)0, (uint32_t)0);
2736 test_true(rc
== MEMCACHED_SUCCESS
);
2738 value
= memcached_get(memc
, key
, strlen(key
),
2739 &length
, &flags
, &rc
);
2741 test_true(rc
== MEMCACHED_SUCCESS
);
2742 test_true(value
== NULL
);
2743 test_true(length
== 0);
2744 test_true(flags
== 0);
2746 value
= memcached_get(memc
, key
, strlen(key
),
2747 &length
, &flags
, &rc
);
2749 test_true(rc
== MEMCACHED_SUCCESS
);
2750 test_true(value
== NULL
);
2751 test_true(length
== 0);
2752 test_true(flags
== 0);
2755 return TEST_SUCCESS
;
2758 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2759 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2761 memcached_return_t rc
;
2762 const char *key
= "mykey";
2767 rc
= memcached_set(memc
, key
, strlen(key
),
2769 (time_t)0, UINT32_MAX
);
2771 test_true(rc
== MEMCACHED_SUCCESS
);
2773 value
= memcached_get(memc
, key
, strlen(key
),
2774 &length
, &flags
, &rc
);
2776 test_true(rc
== MEMCACHED_SUCCESS
);
2777 test_true(value
== NULL
);
2778 test_true(length
== 0);
2779 test_true(flags
== UINT32_MAX
);
2781 return TEST_SUCCESS
;
2785 /* Check the validity of chinese key*/
2786 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2788 memcached_return_t rc
;
2789 const char *key
= "豆瓣";
2790 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2795 rc
= memcached_set(memc
, key
, strlen(key
),
2796 value
, strlen(value
),
2799 test_true(rc
== MEMCACHED_SUCCESS
);
2801 value2
= memcached_get(memc
, key
, strlen(key
),
2802 &length
, &flags
, &rc
);
2804 test_true(length
==strlen(value
));
2805 test_true(rc
== MEMCACHED_SUCCESS
);
2806 test_true(memcmp(value
, value2
, length
)==0);
2809 return TEST_SUCCESS
;
2817 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2820 const memcached_server_st
*server
;
2821 memcached_return_t res
;
2825 memc
= memcached_create(NULL
);
2826 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2827 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2829 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2831 memcached_free(memc
);
2833 return TEST_SUCCESS
;
2836 /* CAS test from Andei */
2837 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2839 memcached_return_t status
;
2840 memcached_result_st
*result
, result_obj
;
2841 const char *key
= "abc";
2842 size_t key_len
= strlen("abc");
2843 const char *value
= "foobar";
2844 size_t value_len
= strlen(value
);
2846 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2848 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2849 test_true(status
== MEMCACHED_SUCCESS
);
2851 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2852 test_true(status
== MEMCACHED_SUCCESS
);
2854 result
= memcached_result_create(memc
, &result_obj
);
2857 memcached_result_create(memc
, &result_obj
);
2858 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2861 test_true(status
== MEMCACHED_SUCCESS
);
2863 memcached_result_free(result
);
2865 return TEST_SUCCESS
;
2868 #include "ketama_test_cases.h"
2869 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2871 memcached_return_t rc
;
2874 memcached_server_st
*server_pool
;
2879 memc
= memcached_create(NULL
);
2882 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2883 test_true(rc
== MEMCACHED_SUCCESS
);
2885 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2886 test_true(value
== 1);
2888 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2889 test_true(rc
== MEMCACHED_SUCCESS
);
2891 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2892 test_true(value
== MEMCACHED_HASH_MD5
);
2894 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");
2895 memcached_server_push(memc
, server_pool
);
2897 /* verify that the server list was parsed okay. */
2898 test_true(memcached_server_count(memc
) == 8);
2899 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2900 test_true(server_pool
[0].port
== 11211);
2901 test_true(server_pool
[0].weight
== 600);
2902 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2903 test_true(server_pool
[2].port
== 11211);
2904 test_true(server_pool
[2].weight
== 200);
2905 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2906 test_true(server_pool
[7].port
== 11211);
2907 test_true(server_pool
[7].weight
== 100);
2909 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2910 * us test the boundary wraparound.
2912 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2914 /* verify the standard ketama set. */
2915 for (x
= 0; x
< 99; x
++)
2917 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2919 memcached_server_instance_st instance
=
2920 memcached_server_instance_by_position(memc
, server_idx
);
2922 const char *hostname
= memcached_server_name(instance
);
2923 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2926 memcached_server_list_free(server_pool
);
2927 memcached_free(memc
);
2929 return TEST_SUCCESS
;
2932 /* Large mget() of missing keys with binary proto
2934 * If many binary quiet commands (such as getq's in an mget) fill the output
2935 * buffer and the server chooses not to respond, memcached_flush hangs. See
2936 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2939 /* sighandler_t function that always asserts false */
2940 static void fail(int unused
__attribute__((unused
)))
2946 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2951 return TEST_SKIPPED
;
2953 memcached_return_t rc
;
2956 size_t* key_lengths
;
2957 void (*oldalarm
)(int);
2958 memcached_st
*memc_clone
;
2960 memc_clone
= memcached_clone(NULL
, memc
);
2961 test_true(memc_clone
);
2963 /* only binproto uses getq for mget */
2964 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2966 /* empty the cache to ensure misses (hence non-responses) */
2967 rc
= memcached_flush(memc_clone
, 0);
2968 test_true(rc
== MEMCACHED_SUCCESS
);
2970 key_lengths
= calloc(key_count
, sizeof(size_t));
2971 keys
= calloc(key_count
, sizeof(char *));
2973 for (x
= 0; x
< key_count
; x
++)
2977 snprintf(buffer
, 30, "%u", x
);
2978 keys
[x
]= strdup(buffer
);
2979 key_lengths
[x
]= strlen(keys
[x
]);
2982 oldalarm
= signal(SIGALRM
, fail
);
2985 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2986 test_true(rc
== MEMCACHED_SUCCESS
);
2989 signal(SIGALRM
, oldalarm
);
2991 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2993 for (x
= 0; x
< key_count
; x
++)
2998 memcached_free(memc_clone
);
3000 return TEST_SUCCESS
;
3004 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3006 test_return_t test_rc
;
3007 test_rc
= pre_binary(memc
);
3009 if (test_rc
!= TEST_SUCCESS
)
3014 /* should work as of r580 */
3015 rc
= _user_supplied_bug21(memc
, 10);
3016 test_true(rc
== TEST_SUCCESS
);
3018 /* should fail as of r580 */
3019 rc
= _user_supplied_bug21(memc
, 1000);
3020 test_true(rc
== TEST_SUCCESS
);
3022 return TEST_SUCCESS
;
3025 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3028 memcached_server_instance_st instance
;
3030 memcached_return_t rc
;
3031 memcached_st
*memc
= memcached_create(NULL
);
3034 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3035 test_true(rc
== MEMCACHED_SUCCESS
);
3037 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3038 test_true(value
== 1);
3040 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3041 test_true(rc
== MEMCACHED_SUCCESS
);
3043 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3044 test_true(value
== MEMCACHED_HASH_MD5
);
3046 /* server should be removed when in delay */
3047 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3048 test_true(rc
== MEMCACHED_SUCCESS
);
3050 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3051 test_true(value
== 1);
3053 memcached_server_st
*server_pool
;
3054 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");
3055 memcached_server_push(memc
, server_pool
);
3057 /* verify that the server list was parsed okay. */
3058 test_true(memcached_server_count(memc
) == 8);
3059 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3060 test_true(server_pool
[0].port
== 11211);
3061 test_true(server_pool
[0].weight
== 600);
3062 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3063 test_true(server_pool
[2].port
== 11211);
3064 test_true(server_pool
[2].weight
== 200);
3065 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3066 test_true(server_pool
[7].port
== 11211);
3067 test_true(server_pool
[7].weight
== 100);
3069 instance
= memcached_server_instance_by_position(memc
, 2);
3070 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3071 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3074 This would not work if there were only two hosts.
3076 for (size_t x
= 0; x
< 99; x
++)
3078 memcached_autoeject(memc
);
3079 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3080 test_true(server_idx
!= 2);
3083 /* and re-added when it's back. */
3084 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3085 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3086 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3087 memc
->distribution
);
3088 for (size_t x
= 0; x
< 99; x
++)
3090 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3091 // We re-use instance from above.
3093 memcached_server_instance_by_position(memc
, server_idx
);
3094 const char *hostname
= memcached_server_name(instance
);
3095 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3098 memcached_server_list_free(server_pool
);
3099 memcached_free(memc
);
3101 return TEST_SUCCESS
;
3104 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3108 memcached_return_t rc
;
3109 memcached_st
*memc
= memcached_create(NULL
);
3113 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3114 test_true(rc
== MEMCACHED_SUCCESS
);
3116 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3117 test_true(value
== 1);
3119 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3120 test_true(rc
== MEMCACHED_SUCCESS
);
3122 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3123 test_true(value
== MEMCACHED_HASH_MD5
);
3126 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3128 memcached_server_st
*server_pool
;
3129 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");
3130 memcached_server_push(memc
, server_pool
);
3132 // @todo this needs to be refactored to actually test something.
3135 if ((fp
= fopen("ketama_keys.txt", "w")))
3139 printf("cannot write to file ketama_keys.txt");
3140 return TEST_FAILURE
;
3143 for (int x
= 0; x
< 10000; x
++)
3146 sprintf(key
, "%d", x
);
3148 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3149 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3150 in_port_t port
= memc
->hosts
[server_idx
].port
;
3151 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3152 memcached_server_instance_st instance
=
3153 memcached_server_instance_by_position(memc
, host_index
);
3157 memcached_server_list_free(server_pool
);
3158 memcached_free(memc
);
3160 return TEST_SUCCESS
;
3164 static test_return_t
result_static(memcached_st
*memc
)
3166 memcached_result_st result
;
3167 memcached_result_st
*result_ptr
;
3169 result_ptr
= memcached_result_create(memc
, &result
);
3170 test_true(result
.options
.is_allocated
== false);
3171 test_true(memcached_is_initialized(&result
) == true);
3172 test_true(result_ptr
);
3173 test_true(result_ptr
== &result
);
3175 memcached_result_free(&result
);
3177 test_true(result
.options
.is_allocated
== false);
3178 test_true(memcached_is_initialized(&result
) == false);
3180 return TEST_SUCCESS
;
3183 static test_return_t
result_alloc(memcached_st
*memc
)
3185 memcached_result_st
*result_ptr
;
3187 result_ptr
= memcached_result_create(memc
, NULL
);
3188 test_true(result_ptr
);
3189 test_true(result_ptr
->options
.is_allocated
== true);
3190 test_true(memcached_is_initialized(result_ptr
) == true);
3191 memcached_result_free(result_ptr
);
3193 return TEST_SUCCESS
;
3196 static test_return_t
string_static_null(memcached_st
*memc
)
3198 memcached_string_st string
;
3199 memcached_string_st
*string_ptr
;
3201 string_ptr
= memcached_string_create(memc
, &string
, 0);
3202 test_true(string
.options
.is_initialized
== true);
3203 test_true(string_ptr
);
3205 /* The following two better be the same! */
3206 test_true(memcached_is_allocated(string_ptr
) == false);
3207 test_true(memcached_is_allocated(&string
) == false);
3208 test_true(&string
== string_ptr
);
3210 test_true(string
.options
.is_initialized
== true);
3211 test_true(memcached_is_initialized(&string
) == true);
3212 memcached_string_free(&string
);
3213 test_true(memcached_is_initialized(&string
) == false);
3215 return TEST_SUCCESS
;
3218 static test_return_t
string_alloc_null(memcached_st
*memc
)
3220 memcached_string_st
*string
;
3222 string
= memcached_string_create(memc
, NULL
, 0);
3224 test_true(memcached_is_allocated(string
) == true);
3225 test_true(memcached_is_initialized(string
) == true);
3226 memcached_string_free(string
);
3228 return TEST_SUCCESS
;
3231 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3233 memcached_string_st
*string
;
3235 string
= memcached_string_create(memc
, NULL
, 1024);
3237 test_true(memcached_is_allocated(string
) == true);
3238 test_true(memcached_is_initialized(string
) == true);
3239 memcached_string_free(string
);
3241 return TEST_SUCCESS
;
3244 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3246 memcached_string_st
*string
;
3248 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3249 test_true(string
== NULL
);
3251 return TEST_SUCCESS
;
3254 static test_return_t
string_alloc_append(memcached_st
*memc
)
3257 char buffer
[SMALL_STRING_LEN
];
3258 memcached_string_st
*string
;
3260 /* Ring the bell! */
3261 memset(buffer
, 6, SMALL_STRING_LEN
);
3263 string
= memcached_string_create(memc
, NULL
, 100);
3265 test_true(memcached_is_allocated(string
) == true);
3266 test_true(memcached_is_initialized(string
) == true);
3268 for (x
= 0; x
< 1024; x
++)
3270 memcached_return_t rc
;
3271 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3272 test_true(rc
== MEMCACHED_SUCCESS
);
3274 test_true(memcached_is_allocated(string
) == true);
3275 memcached_string_free(string
);
3277 return TEST_SUCCESS
;
3280 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3282 memcached_return_t rc
;
3284 char buffer
[SMALL_STRING_LEN
];
3285 memcached_string_st
*string
;
3287 /* Ring the bell! */
3288 memset(buffer
, 6, SMALL_STRING_LEN
);
3290 string
= memcached_string_create(memc
, NULL
, 100);
3292 test_true(memcached_is_allocated(string
) == true);
3293 test_true(memcached_is_initialized(string
) == true);
3295 for (x
= 0; x
< 1024; x
++)
3297 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3298 test_true(rc
== MEMCACHED_SUCCESS
);
3300 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3301 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3302 test_true(memcached_is_allocated(string
) == true);
3303 memcached_string_free(string
);
3305 return TEST_SUCCESS
;
3308 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3310 pairs_free(global_pairs
);
3312 return TEST_SUCCESS
;
3315 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3317 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3318 global_count
= GLOBAL_COUNT
;
3320 for (size_t x
= 0; x
< global_count
; x
++)
3322 global_keys
[x
]= global_pairs
[x
].key
;
3323 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3326 return TEST_SUCCESS
;
3329 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3331 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3332 global_count
= GLOBAL2_COUNT
;
3334 for (size_t x
= 0; x
< global_count
; x
++)
3336 global_keys
[x
]= global_pairs
[x
].key
;
3337 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3340 return TEST_SUCCESS
;
3343 static test_return_t
generate_data(memcached_st
*memc
)
3345 execute_set(memc
, global_pairs
, global_count
);
3347 return TEST_SUCCESS
;
3350 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3352 memcached_stat_st
*stat_p
;
3353 memcached_return_t rc
;
3354 uint32_t host_index
= 0;
3355 execute_set(memc
, global_pairs
, global_count
);
3357 //TODO: hosts used size stats
3358 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3361 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3363 /* This test was changes so that "make test" would work properlly */
3365 memcached_server_instance_st instance
=
3366 memcached_server_instance_by_position(memc
, host_index
);
3368 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3370 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3373 memcached_stat_free(NULL
, stat_p
);
3375 return TEST_SUCCESS
;
3377 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3382 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3383 generate_data(memc
);
3385 return TEST_SUCCESS
;
3388 static test_return_t
get_read_count(memcached_st
*memc
)
3390 memcached_return_t rc
;
3391 memcached_st
*memc_clone
;
3393 memc_clone
= memcached_clone(NULL
, memc
);
3394 test_true(memc_clone
);
3396 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3400 size_t return_value_length
;
3404 for (size_t x
= count
= 0; x
< global_count
; x
++)
3406 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3407 &return_value_length
, &flags
, &rc
);
3408 if (rc
== MEMCACHED_SUCCESS
)
3417 memcached_free(memc_clone
);
3419 return TEST_SUCCESS
;
3422 static test_return_t
get_read(memcached_st
*memc
)
3424 memcached_return_t rc
;
3428 size_t return_value_length
;
3431 for (size_t x
= 0; x
< global_count
; x
++)
3433 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3434 &return_value_length
, &flags
, &rc
);
3436 test_true(return_value);
3437 test_true(rc == MEMCACHED_SUCCESS);
3439 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3444 return TEST_SUCCESS
;
3447 static test_return_t
mget_read(memcached_st
*memc
)
3449 memcached_return_t rc
;
3451 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3452 test_true(rc
== MEMCACHED_SUCCESS
);
3453 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3455 return TEST_SUCCESS
;
3458 static test_return_t
mget_read_result(memcached_st
*memc
)
3460 memcached_return_t rc
;
3462 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3463 test_true(rc
== MEMCACHED_SUCCESS
);
3464 /* Turn this into a help function */
3466 memcached_result_st results_obj
;
3467 memcached_result_st
*results
;
3469 results
= memcached_result_create(memc
, &results_obj
);
3471 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3474 test_true(rc
== MEMCACHED_SUCCESS
);
3477 memcached_result_free(&results_obj
);
3480 return TEST_SUCCESS
;
3483 static test_return_t
mget_read_function(memcached_st
*memc
)
3485 memcached_return_t rc
;
3487 memcached_execute_fn callbacks
[1];
3489 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3490 test_true(rc
== MEMCACHED_SUCCESS
);
3492 callbacks
[0]= &callback_counter
;
3494 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3496 return TEST_SUCCESS
;
3499 static test_return_t
delete_generate(memcached_st
*memc
)
3501 for (size_t x
= 0; x
< global_count
; x
++)
3503 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3506 return TEST_SUCCESS
;
3509 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3514 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3516 for (size_t x
= 0; x
< global_count
; x
++)
3518 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3521 return TEST_SUCCESS
;
3524 static test_return_t
add_host_test1(memcached_st
*memc
)
3526 memcached_return_t rc
;
3527 char servername
[]= "0.example.com";
3528 memcached_server_st
*servers
;
3530 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3532 test_true(1 == memcached_server_list_count(servers
));
3534 for (size_t x
= 2; x
< 20; x
++)
3536 char buffer
[SMALL_STRING_LEN
];
3538 snprintf(buffer
, SMALL_STRING_LEN
, "%zu.example.com", 400+x
);
3539 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3541 test_true(rc
== MEMCACHED_SUCCESS
);
3542 test_true(x
== memcached_server_list_count(servers
));
3545 rc
= memcached_server_push(memc
, servers
);
3546 test_true(rc
== MEMCACHED_SUCCESS
);
3547 rc
= memcached_server_push(memc
, servers
);
3548 test_true(rc
== MEMCACHED_SUCCESS
);
3550 memcached_server_list_free(servers
);
3552 return TEST_SUCCESS
;
3555 static test_return_t
pre_nonblock(memcached_st
*memc
)
3557 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3559 return TEST_SUCCESS
;
3562 static test_return_t
pre_cork(memcached_st
*memc
)
3564 memcached_return_t rc
;
3567 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3570 return TEST_SKIPPED
;
3573 if (rc
== MEMCACHED_SUCCESS
)
3574 return TEST_SUCCESS
;
3576 return TEST_SKIPPED
;
3579 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3586 return TEST_SKIPPED
;
3589 if (rc
!= TEST_SUCCESS
)
3592 return pre_nonblock(memc
);
3595 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3597 memcached_return_t rc
= MEMCACHED_FAILURE
;
3598 memcached_st
*memc_clone
;
3600 memc_clone
= memcached_clone(NULL
, memc
);
3601 test_true(memc_clone
);
3602 // The memcached_version needs to be done on a clone, because the server
3603 // will not toggle protocol on an connection.
3604 memcached_version(memc_clone
);
3606 if (libmemcached_util_version_check(memc_clone
, 1, 3, 0))
3608 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3609 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3610 test_true(rc
== MEMCACHED_SUCCESS
);
3611 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3615 return TEST_SKIPPED
;
3618 memcached_free(memc_clone
);
3620 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3623 static test_return_t
pre_murmur(memcached_st
*memc
)
3625 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3627 return TEST_SUCCESS
;
3630 static test_return_t
pre_jenkins(memcached_st
*memc
)
3632 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3634 return TEST_SUCCESS
;
3638 static test_return_t
pre_md5(memcached_st
*memc
)
3640 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3642 return TEST_SUCCESS
;
3645 static test_return_t
pre_crc(memcached_st
*memc
)
3647 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3649 return TEST_SUCCESS
;
3652 static test_return_t
pre_hsieh(memcached_st
*memc
)
3654 #ifdef HAVE_HSIEH_HASH
3655 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3656 return TEST_SUCCESS
;
3659 return TEST_SKIPPED
;
3663 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3667 return TEST_SUCCESS
;
3670 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3672 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3674 return TEST_SUCCESS
;
3677 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3679 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3681 return TEST_SUCCESS
;
3684 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3686 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3688 return TEST_SUCCESS
;
3691 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3693 memcached_return_t rc
;
3696 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3697 test_true(rc
== MEMCACHED_SUCCESS
);
3699 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3700 test_true(value
== 1);
3702 return TEST_SUCCESS
;
3705 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3707 memcached_return_t rc
;
3710 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3711 test_true(rc
== MEMCACHED_SUCCESS
);
3713 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3714 test_true(value
== 1);
3716 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3717 test_true(rc
== MEMCACHED_SUCCESS
);
3719 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3720 test_true(value
== MEMCACHED_HASH_MD5
);
3722 return TEST_SUCCESS
;
3726 @note This should be testing to see if the server really supports the binary protocol.
3728 static test_return_t
pre_binary(memcached_st
*memc
)
3730 memcached_return_t rc
= MEMCACHED_FAILURE
;
3732 if (libmemcached_util_version_check(memc
, 1, 3, 0))
3734 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3735 test_true(rc
== MEMCACHED_SUCCESS
);
3736 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3739 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3742 static test_return_t
pre_sasl(memcached_st
*memc
)
3744 memcached_return_t rc
= MEMCACHED_FAILURE
;
3746 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3747 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3748 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3749 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3751 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3753 memcached_server_st
*servers
= memcached_servers_parse(server
);
3754 test_true(servers
!= NULL
);
3755 memcached_servers_reset(memc
);
3756 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3757 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3758 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3759 test_true(rc
== MEMCACHED_SUCCESS
);
3765 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3768 static test_return_t
pre_replication(memcached_st
*memc
)
3770 test_return_t test_rc
;
3771 test_rc
= pre_binary(memc
);
3773 if (test_rc
!= TEST_SUCCESS
)
3777 * Make sure that we store the item on all servers
3778 * (master + replicas == number of servers)
3780 memcached_return_t rc
;
3781 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3782 memcached_server_count(memc
) - 1);
3783 test_true(rc
== MEMCACHED_SUCCESS
);
3784 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3786 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3790 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3794 rc
= pre_replication(memc
);
3795 if (rc
!= TEST_SUCCESS
)
3798 rc
= pre_nonblock(memc
);
3804 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3807 #ifdef HARD_MALLOC_TESTS
3808 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3816 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3819 #ifdef HARD_MALLOC_TESTS
3820 void *ret
= malloc(size
+ 8);
3823 ret
= (void*)((caddr_t
)ret
+ 8);
3826 void *ret
= malloc(size
);
3831 memset(ret
, 0xff, size
);
3838 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3841 #ifdef HARD_MALLOC_TESTS
3842 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3843 void *nmem
= realloc(real_ptr
, size
+ 8);
3848 ret
= (void*)((caddr_t
)nmem
+ 8);
3853 return realloc(mem
, size
);
3858 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3861 #ifdef HARD_MALLOC_TESTS
3862 void *mem
= my_malloc(ptr
, nelem
* size
);
3865 memset(mem
, 0, nelem
* size
);
3870 return calloc(nelem
, size
);
3875 static test_return_t
set_prefix(memcached_st
*memc
)
3877 memcached_return_t rc
;
3878 const char *key
= "mine";
3881 /* Make sure be default none exists */
3882 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3883 test_true(rc
== MEMCACHED_FAILURE
);
3885 /* Test a clean set */
3886 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3887 test_true(rc
== MEMCACHED_SUCCESS
);
3889 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3890 test_true(memcmp(value
, key
, 4) == 0);
3891 test_true(rc
== MEMCACHED_SUCCESS
);
3893 /* Test that we can turn it off */
3894 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3895 test_true(rc
== MEMCACHED_SUCCESS
);
3897 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3898 test_true(rc
== MEMCACHED_FAILURE
);
3900 /* Now setup for main test */
3901 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3902 test_true(rc
== MEMCACHED_SUCCESS
);
3904 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3905 test_true(rc
== MEMCACHED_SUCCESS
);
3906 test_true(memcmp(value
, key
, 4) == 0);
3908 /* Set to Zero, and then Set to something too large */
3911 memset(long_key
, 0, 255);
3913 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3914 test_true(rc
== MEMCACHED_SUCCESS
);
3916 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3917 test_true(rc
== MEMCACHED_FAILURE
);
3918 test_true(value
== NULL
);
3920 /* Test a long key for failure */
3921 /* TODO, extend test to determine based on setting, what result should be */
3922 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3923 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3924 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3925 test_true(rc
== MEMCACHED_SUCCESS
);
3927 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3928 strcpy(long_key
, "This is more then the allotted number of characters");
3929 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3930 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3932 /* Test for a bad prefix, but with a short key */
3933 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3934 test_true(rc
== MEMCACHED_SUCCESS
);
3936 strcpy(long_key
, "dog cat");
3937 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3938 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3941 return TEST_SUCCESS
;
3945 #ifdef MEMCACHED_ENABLE_DEPRECATED
3946 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3948 void *test_ptr
= NULL
;
3951 memcached_malloc_fn malloc_cb
=
3952 (memcached_malloc_fn
)my_malloc
;
3953 cb_ptr
= *(void **)&malloc_cb
;
3954 memcached_return_t rc
;
3956 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3957 test_true(rc
== MEMCACHED_SUCCESS
);
3958 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3959 test_true(rc
== MEMCACHED_SUCCESS
);
3960 test_true(test_ptr
== cb_ptr
);
3964 memcached_realloc_fn realloc_cb
=
3965 (memcached_realloc_fn
)my_realloc
;
3966 cb_ptr
= *(void **)&realloc_cb
;
3967 memcached_return_t rc
;
3969 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3970 test_true(rc
== MEMCACHED_SUCCESS
);
3971 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3972 test_true(rc
== MEMCACHED_SUCCESS
);
3973 test_true(test_ptr
== cb_ptr
);
3977 memcached_free_fn free_cb
=
3978 (memcached_free_fn
)my_free
;
3979 cb_ptr
= *(void **)&free_cb
;
3980 memcached_return_t rc
;
3982 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3983 test_true(rc
== MEMCACHED_SUCCESS
);
3984 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3985 test_true(rc
== MEMCACHED_SUCCESS
);
3986 test_true(test_ptr
== cb_ptr
);
3989 return TEST_SUCCESS
;
3994 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3996 memcached_return_t rc
;
3997 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3998 my_realloc
, my_calloc
, NULL
);
3999 test_true(rc
== MEMCACHED_FAILURE
);
4001 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4002 my_realloc
, my_calloc
, NULL
);
4004 memcached_malloc_fn mem_malloc
;
4005 memcached_free_fn mem_free
;
4006 memcached_realloc_fn mem_realloc
;
4007 memcached_calloc_fn mem_calloc
;
4008 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4009 &mem_realloc
, &mem_calloc
);
4011 test_true(mem_malloc
== my_malloc
);
4012 test_true(mem_realloc
== my_realloc
);
4013 test_true(mem_calloc
== my_calloc
);
4014 test_true(mem_free
== my_free
);
4016 return TEST_SUCCESS
;
4019 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4022 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4023 memcached_hash_t hash
;
4024 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4025 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4028 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4029 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4031 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4033 if (hash
!= MEMCACHED_HASH_CRC
)
4034 return TEST_SKIPPED
;
4036 return TEST_SUCCESS
;
4039 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4042 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4043 memcached_hash_t hash
;
4044 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4045 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4048 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4049 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4051 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4053 if (hash
!= MEMCACHED_HASH_HSIEH
)
4054 return TEST_SKIPPED
;
4057 return TEST_SUCCESS
;
4060 static test_return_t
enable_cas(memcached_st
*memc
)
4062 unsigned int set
= 1;
4064 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4066 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4068 return TEST_SUCCESS
;
4071 return TEST_SKIPPED
;
4074 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4076 memcached_version(memc
);
4078 memcached_server_instance_st instance
=
4079 memcached_server_instance_by_position(memc
, 0);
4081 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4082 || instance
->minor_version
> 2)
4084 return TEST_SUCCESS
;
4087 return TEST_SKIPPED
;
4090 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4092 memcached_return_t rc
;
4095 memcached_servers_reset(memc
);
4097 if (stat("/tmp/memcached.socket", &buf
))
4098 return TEST_SKIPPED
;
4100 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4102 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4105 static test_return_t
pre_nodelay(memcached_st
*memc
)
4107 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4108 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4110 return TEST_SUCCESS
;
4113 static test_return_t
pre_settimer(memcached_st
*memc
)
4115 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4118 return TEST_SUCCESS
;
4121 static test_return_t
poll_timeout(memcached_st
*memc
)
4127 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4129 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4131 test_true(timeout
== 100);
4133 return TEST_SUCCESS
;
4136 static test_return_t
noreply_test(memcached_st
*memc
)
4138 memcached_return_t ret
;
4139 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4140 test_true(ret
== MEMCACHED_SUCCESS
);
4141 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4142 test_true(ret
== MEMCACHED_SUCCESS
);
4143 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4144 test_true(ret
== MEMCACHED_SUCCESS
);
4145 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4146 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4147 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4149 for (int count
=0; count
< 5; ++count
)
4151 for (size_t x
= 0; x
< 100; ++x
)
4154 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4158 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4161 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4164 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4167 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4170 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4176 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4180 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4181 ** API and is _ONLY_ done this way to verify that the library works the
4182 ** way it is supposed to do!!!!
4185 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4187 memcached_server_instance_st instance
=
4188 memcached_server_instance_by_position(memc
, x
);
4189 no_msg
+=(int)(instance
->cursor_active
);
4192 test_true(no_msg
== 0);
4193 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4196 ** Now validate that all items was set properly!
4198 for (size_t x
= 0; x
< 100; ++x
)
4202 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4205 char* value
=memcached_get(memc
, key
, strlen(key
),
4206 &length
, &flags
, &ret
);
4207 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4210 case 0: /* FALLTHROUGH */
4211 case 1: /* FALLTHROUGH */
4213 test_true(strncmp(value
, key
, len
) == 0);
4214 test_true(len
== length
);
4217 test_true(length
== len
* 2);
4220 test_true(length
== len
* 3);
4230 /* Try setting an illegal cas value (should not return an error to
4231 * the caller (because we don't expect a return message from the server)
4233 const char* keys
[]= {"0"};
4234 size_t lengths
[]= {1};
4237 memcached_result_st results_obj
;
4238 memcached_result_st
*results
;
4239 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4240 test_true(ret
== MEMCACHED_SUCCESS
);
4242 results
= memcached_result_create(memc
, &results_obj
);
4244 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4246 test_true(ret
== MEMCACHED_SUCCESS
);
4247 uint64_t cas
= memcached_result_cas(results
);
4248 memcached_result_free(&results_obj
);
4250 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4251 test_true(ret
== MEMCACHED_SUCCESS
);
4254 * The item will have a new cas value, so try to set it again with the old
4255 * value. This should fail!
4257 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4258 test_true(ret
== MEMCACHED_SUCCESS
);
4259 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4260 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4261 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4264 return TEST_SUCCESS
;
4267 static test_return_t
analyzer_test(memcached_st
*memc
)
4269 memcached_return_t rc
;
4270 memcached_stat_st
*memc_stat
;
4271 memcached_analysis_st
*report
;
4273 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4274 test_true(rc
== MEMCACHED_SUCCESS
);
4275 test_true(memc_stat
);
4277 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4278 test_true(rc
== MEMCACHED_SUCCESS
);
4282 memcached_stat_free(NULL
, memc_stat
);
4284 return TEST_SUCCESS
;
4287 /* Count the objects */
4288 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4289 const char *key
__attribute__((unused
)),
4290 size_t key_length
__attribute__((unused
)),
4293 size_t *counter
= (size_t *)context
;
4295 *counter
= *counter
+ 1;
4297 return MEMCACHED_SUCCESS
;
4300 static test_return_t
dump_test(memcached_st
*memc
)
4302 memcached_return_t rc
;
4304 memcached_dump_fn callbacks
[1];
4305 test_return_t main_rc
;
4307 callbacks
[0]= &callback_dump_counter
;
4309 /* No support for Binary protocol yet */
4310 if (memc
->flags
.binary_protocol
)
4311 return TEST_SUCCESS
;
4313 main_rc
= set_test3(memc
);
4315 test_true (main_rc
== TEST_SUCCESS
);
4317 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4318 test_true(rc
== MEMCACHED_SUCCESS
);
4320 /* We may have more then 32 if our previous flush has not completed */
4321 test_true(counter
>= 32);
4323 return TEST_SUCCESS
;
4326 #ifdef HAVE_LIBMEMCACHEDUTIL
4327 static void* connection_release(void *arg
)
4330 memcached_pool_st
* pool
;
4335 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4339 static test_return_t
connection_pool_test(memcached_st
*memc
)
4341 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4342 test_true(pool
!= NULL
);
4343 memcached_st
* mmc
[10];
4344 memcached_return_t rc
;
4346 for (size_t x
= 0; x
< 10; ++x
)
4348 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4349 test_true(mmc
[x
] != NULL
);
4350 test_true(rc
== MEMCACHED_SUCCESS
);
4353 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4354 test_true(rc
== MEMCACHED_SUCCESS
);
4358 memcached_pool_st
* pool
;
4360 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4361 pthread_create(&tid
, NULL
, connection_release
, &item
);
4362 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4363 test_true(rc
== MEMCACHED_SUCCESS
);
4364 pthread_join(tid
, NULL
);
4365 test_true(mmc
[9] == item
.mmc
);
4366 const char *key
= "key";
4367 size_t keylen
= strlen(key
);
4369 // verify that I can do ops with all connections
4370 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4371 test_true(rc
== MEMCACHED_SUCCESS
);
4373 for (size_t x
= 0; x
< 10; ++x
)
4375 uint64_t number_value
;
4376 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4377 test_true(rc
== MEMCACHED_SUCCESS
);
4378 test_true(number_value
== (x
+1));
4382 for (size_t x
= 0; x
< 10; ++x
)
4384 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4388 /* verify that I can set behaviors on the pool when I don't have all
4389 * of the connections in the pool. It should however be enabled
4390 * when I push the item into the pool
4392 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4393 test_true(mmc
[0] != NULL
);
4395 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4396 test_true(rc
== MEMCACHED_SUCCESS
);
4398 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4399 test_true(mmc
[1] != NULL
);
4401 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4402 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4403 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4405 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4406 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4407 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4410 test_true(memcached_pool_destroy(pool
) == memc
);
4411 return TEST_SUCCESS
;
4414 static test_return_t
util_version_test(memcached_st
*memc
)
4418 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4419 test_true(if_successful
== true);
4421 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4422 test_true(if_successful
== false);
4424 memcached_server_instance_st instance
=
4425 memcached_server_instance_by_position(memc
, 0);
4427 memcached_version(memc
);
4429 // We only use one binary when we test, so this should be just fine.
4430 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4431 test_true(if_successful
== true);
4433 if (instance
->micro_version
> 0)
4434 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4435 else if (instance
->minor_version
> 0)
4436 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4437 else if (instance
->major_version
> 0)
4438 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4440 test_true(if_successful
== true);
4442 if (instance
->micro_version
> 0)
4443 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4444 else if (instance
->minor_version
> 0)
4445 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4446 else if (instance
->major_version
> 0)
4447 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4449 test_true(if_successful
== false);
4451 return TEST_SUCCESS
;
4454 static test_return_t
ping_test(memcached_st
*memc
)
4456 memcached_return_t rc
;
4457 memcached_server_instance_st instance
=
4458 memcached_server_instance_by_position(memc
, 0);
4460 // Test both the version that returns a code, and the one that does not.
4461 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4462 memcached_server_port(instance
), NULL
));
4464 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4465 memcached_server_port(instance
), &rc
));
4467 test_true(rc
== MEMCACHED_SUCCESS
);
4469 return TEST_SUCCESS
;
4473 static test_return_t
replication_set_test(memcached_st
*memc
)
4475 memcached_return_t rc
;
4476 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4477 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4479 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4480 test_true(rc
== MEMCACHED_SUCCESS
);
4483 ** We are using the quiet commands to store the replicas, so we need
4484 ** to ensure that all of them are processed before we can continue.
4485 ** In the test we go directly from storing the object to trying to
4486 ** receive the object from all of the different servers, so we
4487 ** could end up in a race condition (the memcached server hasn't yet
4488 ** processed the quiet command from the replication set when it process
4489 ** the request from the other client (created by the clone)). As a
4490 ** workaround for that we call memcached_quit to send the quit command
4491 ** to the server and wait for the response ;-) If you use the test code
4492 ** as an example for your own code, please note that you shouldn't need
4495 memcached_quit(memc
);
4498 ** "bubba" should now be stored on all of our servers. We don't have an
4499 ** easy to use API to address each individual server, so I'll just iterate
4500 ** through a bunch of "master keys" and I should most likely hit all of the
4503 for (int x
= 'a'; x
<= 'z'; ++x
)
4505 char key
[2]= { [0]= (char)x
};
4508 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4510 test_true(rc
== MEMCACHED_SUCCESS
);
4511 test_true(val
!= NULL
);
4515 memcached_free(memc_clone
);
4517 return TEST_SUCCESS
;
4520 static test_return_t
replication_get_test(memcached_st
*memc
)
4522 memcached_return_t rc
;
4525 * Don't do the following in your code. I am abusing the internal details
4526 * within the library, and this is not a supported interface.
4527 * This is to verify correct behavior in the library
4529 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4531 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4532 memcached_server_instance_st instance
=
4533 memcached_server_instance_by_position(memc_clone
, host
);
4535 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4537 for (int x
= 'a'; x
<= 'z'; ++x
)
4539 char key
[2]= { [0]= (char)x
};
4542 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4544 test_true(rc
== MEMCACHED_SUCCESS
);
4545 test_true(val
!= NULL
);
4549 memcached_free(memc_clone
);
4552 return TEST_SUCCESS
;
4555 static test_return_t
replication_mget_test(memcached_st
*memc
)
4557 memcached_return_t rc
;
4558 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4559 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4561 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4562 size_t len
[]= { 5, 4, 4, 4 };
4564 for (size_t x
= 0; x
< 4; ++x
)
4566 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4567 test_true(rc
== MEMCACHED_SUCCESS
);
4571 ** We are using the quiet commands to store the replicas, so we need
4572 ** to ensure that all of them are processed before we can continue.
4573 ** In the test we go directly from storing the object to trying to
4574 ** receive the object from all of the different servers, so we
4575 ** could end up in a race condition (the memcached server hasn't yet
4576 ** processed the quiet command from the replication set when it process
4577 ** the request from the other client (created by the clone)). As a
4578 ** workaround for that we call memcached_quit to send the quit command
4579 ** to the server and wait for the response ;-) If you use the test code
4580 ** as an example for your own code, please note that you shouldn't need
4583 memcached_quit(memc
);
4586 * Don't do the following in your code. I am abusing the internal details
4587 * within the library, and this is not a supported interface.
4588 * This is to verify correct behavior in the library
4590 memcached_result_st result_obj
;
4591 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4593 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4594 memcached_server_instance_st instance
=
4595 memcached_server_instance_by_position(new_clone
, host
);
4596 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4598 for (int x
= 'a'; x
<= 'z'; ++x
)
4600 char key
[2]= { [0]= (char)x
, [1]= 0 };
4602 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4603 test_true(rc
== MEMCACHED_SUCCESS
);
4605 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4609 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4613 test_true(hits
== 4);
4614 memcached_result_free(&result_obj
);
4617 memcached_free(new_clone
);
4620 memcached_free(memc_clone
);
4622 return TEST_SUCCESS
;
4625 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4627 memcached_result_st result_obj
;
4628 memcached_return_t rc
;
4629 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4630 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4631 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4633 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4634 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4636 for (size_t x
= 0; x
< 7; ++x
)
4638 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4639 test_true(rc
== MEMCACHED_SUCCESS
);
4642 memcached_quit(memc
);
4644 for (size_t x
= 0; x
< 7; ++x
)
4646 const char key
[2]= { [0]= (const char)x
};
4648 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4649 test_true(rc
== MEMCACHED_SUCCESS
);
4651 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4655 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4659 test_true(hits
== 7);
4660 memcached_result_free(&result_obj
);
4662 memcached_free(memc_clone
);
4663 return TEST_SUCCESS
;
4666 static test_return_t
replication_delete_test(memcached_st
*memc
)
4668 memcached_return_t rc
;
4669 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4670 /* Delete the items from all of the servers except 1 */
4671 uint64_t repl
= memcached_behavior_get(memc
,
4672 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4673 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4675 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4676 size_t len
[]= { 5, 4, 4, 4 };
4678 for (size_t x
= 0; x
< 4; ++x
)
4680 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4681 test_true(rc
== MEMCACHED_SUCCESS
);
4685 * Don't do the following in your code. I am abusing the internal details
4686 * within the library, and this is not a supported interface.
4687 * This is to verify correct behavior in the library
4689 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4690 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4692 memcached_server_instance_st instance
=
4693 memcached_server_instance_by_position(memc_clone
, x
);
4695 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4696 if (++hash
== memc_clone
->number_of_hosts
)
4700 memcached_result_st result_obj
;
4701 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4703 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4705 const char key
[2]= { [0]= (const char)x
};
4707 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4708 test_true(rc
== MEMCACHED_SUCCESS
);
4710 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4714 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4718 test_true(hits
== 4);
4719 memcached_result_free(&result_obj
);
4722 memcached_free(memc_clone
);
4724 return TEST_SUCCESS
;
4728 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4732 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4733 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4734 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4735 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4736 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4737 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4738 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4739 #ifdef HAVE_HSIEH_HASH
4740 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4742 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4743 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4744 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4746 return TEST_SUCCESS
;
4750 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4752 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4753 #ifdef HAVE_HSIEH_HASH
4754 expected_rc
= MEMCACHED_SUCCESS
;
4756 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4757 (uint64_t)MEMCACHED_HASH_HSIEH
);
4758 test_true(rc
== expected_rc
);
4760 return TEST_SUCCESS
;
4763 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4768 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4772 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4773 test_true(one_at_a_time_values
[x
] == hash_val
);
4776 return TEST_SUCCESS
;
4779 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4784 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4788 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4789 test_true(md5_values
[x
] == hash_val
);
4792 return TEST_SUCCESS
;
4795 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4800 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4804 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4805 test_true(crc_values
[x
] == hash_val
);
4808 return TEST_SUCCESS
;
4811 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4816 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4820 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4821 test_true(fnv1_64_values
[x
] == hash_val
);
4824 return TEST_SUCCESS
;
4827 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4832 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4836 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4837 test_true(fnv1a_64_values
[x
] == hash_val
);
4840 return TEST_SUCCESS
;
4843 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4849 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4853 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4854 test_true(fnv1_32_values
[x
] == hash_val
);
4857 return TEST_SUCCESS
;
4860 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4865 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4869 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4870 test_true(fnv1a_32_values
[x
] == hash_val
);
4873 return TEST_SUCCESS
;
4876 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4881 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4885 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4886 test_true(hsieh_values
[x
] == hash_val
);
4889 return TEST_SUCCESS
;
4892 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4894 #ifdef WORDS_BIGENDIAN
4895 return TEST_SKIPPED
;
4900 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4904 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4905 test_true(murmur_values
[x
] == hash_val
);
4908 return TEST_SUCCESS
;
4912 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4918 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4922 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4923 test_true(jenkins_values
[x
] == hash_val
);
4926 return TEST_SUCCESS
;
4929 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4932 return libhashkit_md5(string
, string_length
);
4935 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4938 return libhashkit_crc32(string
, string_length
);
4941 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4945 const hashkit_st
*kit
;
4947 hashkit_return_t hash_rc
;
4949 uint32_t md5_hosts
[]= {4U, 1U, 0U, 1U, 4U, 2U, 0U, 3U, 0U, 0U, 3U, 1U, 0U, 0U, 1U, 3U, 0U, 0U, 0U, 3U, 1U, 0U, 4U, 4U, 3U};
4950 uint32_t crc_hosts
[]= {2U, 4U, 1U, 0U, 2U, 4U, 4U, 4U, 1U, 2U, 3U, 4U, 3U, 4U, 1U, 3U, 3U, 2U, 0U, 0U, 0U, 1U, 2U, 4U, 0U};
4952 kit
= memcached_get_hashkit(memc
);
4954 hashkit_clone(&new_kit
, kit
);
4955 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4956 test_true(hash_rc
== HASHKIT_SUCCESS
);
4958 memcached_set_hashkit(memc
, &new_kit
);
4961 Verify Setting the hash.
4963 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4967 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4968 test_true(md5_values
[x
] == hash_val
);
4973 Now check memcached_st.
4975 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4979 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4980 test_true(md5_hosts
[x
] == hash_val
);
4983 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4984 test_true(hash_rc
== HASHKIT_SUCCESS
);
4986 memcached_set_hashkit(memc
, &new_kit
);
4989 Verify Setting the hash.
4991 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4995 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4996 test_true(crc_values
[x
] == hash_val
);
4999 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5003 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5004 test_true(crc_hosts
[x
] == hash_val
);
5007 return TEST_SUCCESS
;
5011 Test case adapted from John Gorman <johngorman2@gmail.com>
5013 We are testing the error condition when we connect to a server via memcached_get()
5014 but find that the server is not available.
5016 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
5019 memcached_st
*tl_memc_h
;
5020 memcached_server_st
*servers
;
5022 const char *key
= "MemcachedLives";
5025 memcached_return rc
;
5029 tl_memc_h
= memcached_create(NULL
);
5030 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5031 memcached_server_push(tl_memc_h
, servers
);
5032 memcached_server_list_free(servers
);
5034 // See if memcached is reachable.
5035 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5038 test_true(len
== 0);
5039 test_true(rc
== MEMCACHED_ERRNO
);
5041 memcached_free(tl_memc_h
);
5043 return TEST_SUCCESS
;
5047 We connect to a server which exists, but search for a key that does not exist.
5049 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5051 const char *key
= "MemcachedKeyNotEXIST";
5054 memcached_return rc
;
5057 // See if memcached is reachable.
5058 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5061 test_true(len
== 0);
5062 test_true(rc
== MEMCACHED_NOTFOUND
);
5064 return TEST_SUCCESS
;
5068 Test case adapted from John Gorman <johngorman2@gmail.com>
5070 We are testing the error condition when we connect to a server via memcached_get_by_key()
5071 but find that the server is not available.
5073 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
5076 memcached_st
*tl_memc_h
;
5077 memcached_server_st
*servers
;
5079 const char *key
= "MemcachedLives";
5082 memcached_return rc
;
5086 tl_memc_h
= memcached_create(NULL
);
5087 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5088 memcached_server_push(tl_memc_h
, servers
);
5089 memcached_server_list_free(servers
);
5091 // See if memcached is reachable.
5092 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5095 test_true(len
== 0);
5096 test_true(rc
== MEMCACHED_ERRNO
);
5098 memcached_free(tl_memc_h
);
5100 return TEST_SUCCESS
;
5104 We connect to a server which exists, but search for a key that does not exist.
5106 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5108 const char *key
= "MemcachedKeyNotEXIST";
5111 memcached_return rc
;
5114 // See if memcached is reachable.
5115 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5118 test_true(len
== 0);
5119 test_true(rc
== MEMCACHED_NOTFOUND
);
5121 return TEST_SUCCESS
;
5125 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5127 memcached_return_t rc
;
5130 memcached_server_st
*server_pool
;
5135 memc
= memcached_create(NULL
);
5138 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5139 test_true(rc
== MEMCACHED_SUCCESS
);
5141 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5142 test_true(value
== 1);
5144 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5145 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5148 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");
5149 memcached_server_push(memc
, server_pool
);
5151 /* verify that the server list was parsed okay. */
5152 test_true(memcached_server_count(memc
) == 8);
5153 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5154 test_true(server_pool
[0].port
== 11211);
5155 test_true(server_pool
[0].weight
== 600);
5156 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5157 test_true(server_pool
[2].port
== 11211);
5158 test_true(server_pool
[2].weight
== 200);
5159 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5160 test_true(server_pool
[7].port
== 11211);
5161 test_true(server_pool
[7].weight
== 100);
5163 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5164 * us test the boundary wraparound.
5166 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5168 /* verify the standard ketama set. */
5169 for (x
= 0; x
< 99; x
++)
5171 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5172 memcached_server_instance_st instance
=
5173 memcached_server_instance_by_position(memc
, server_idx
);
5174 const char *hostname
= memcached_server_name(instance
);
5176 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5179 memcached_server_list_free(server_pool
);
5180 memcached_free(memc
);
5182 return TEST_SUCCESS
;
5185 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5187 memcached_return_t rc
;
5190 memcached_server_st
*server_pool
;
5195 memc
= memcached_create(NULL
);
5198 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5199 test_true(rc
== MEMCACHED_SUCCESS
);
5201 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5202 test_true(value
== 1);
5204 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5205 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5207 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");
5208 memcached_server_push(memc
, server_pool
);
5210 /* verify that the server list was parsed okay. */
5211 test_true(memcached_server_count(memc
) == 8);
5212 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5213 test_true(server_pool
[0].port
== 11211);
5214 test_true(server_pool
[0].weight
== 600);
5215 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5216 test_true(server_pool
[2].port
== 11211);
5217 test_true(server_pool
[2].weight
== 200);
5218 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5219 test_true(server_pool
[7].port
== 11211);
5220 test_true(server_pool
[7].weight
== 100);
5222 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5223 * us test the boundary wraparound.
5225 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5227 /* verify the standard ketama set. */
5228 for (x
= 0; x
< 99; x
++)
5230 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5232 memcached_server_instance_st instance
=
5233 memcached_server_instance_by_position(memc
, server_idx
);
5235 const char *hostname
= memcached_server_name(instance
);
5237 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5240 memcached_server_list_free(server_pool
);
5241 memcached_free(memc
);
5243 return TEST_SUCCESS
;
5246 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5248 test_return_t test_rc
;
5249 test_rc
= pre_binary(memc
);
5251 if (test_rc
!= TEST_SUCCESS
)
5254 memcached_return_t ret
;
5255 const char *key
= "regression_bug_434484";
5256 size_t keylen
= strlen(key
);
5258 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5259 test_true(ret
== MEMCACHED_NOTSTORED
);
5261 size_t size
= 2048 * 1024;
5262 void *data
= calloc(1, size
);
5263 test_true(data
!= NULL
);
5264 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5265 test_true(ret
== MEMCACHED_E2BIG
);
5268 return TEST_SUCCESS
;
5271 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5273 test_return_t test_rc
;
5274 test_rc
= pre_binary(memc
);
5276 if (test_rc
!= TEST_SUCCESS
)
5279 memcached_return_t rc
;
5281 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5284 * I only want to hit only _one_ server so I know the number of requests I'm
5285 * sending in the pipleine to the server. Let's try to do a multiget of
5286 * 1024 (that should satisfy most users don't you think?). Future versions
5287 * will include a mget_execute function call if you need a higher number.
5289 uint32_t number_of_hosts
= memcached_server_count(memc
);
5290 memc
->number_of_hosts
= 1;
5291 const size_t max_keys
= 1024;
5292 char **keys
= calloc(max_keys
, sizeof(char*));
5293 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5295 for (size_t x
= 0; x
< max_keys
; ++x
)
5299 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5301 test_true(keys
[x
] != NULL
);
5305 * Run two times.. the first time we should have 100% cache miss,
5306 * and the second time we should have 100% cache hits
5308 for (size_t y
= 0; y
< 2; y
++)
5310 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5311 test_true(rc
== MEMCACHED_SUCCESS
);
5312 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5316 /* The first iteration should give me a 100% cache miss. verify that*/
5317 char blob
[1024]= { 0 };
5319 test_true(counter
== 0);
5321 for (size_t x
= 0; x
< max_keys
; ++x
)
5323 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5324 blob
, sizeof(blob
), 0, 0);
5325 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5330 /* Verify that we received all of the key/value pairs */
5331 test_true(counter
== max_keys
);
5335 /* Release allocated resources */
5336 for (size_t x
= 0; x
< max_keys
; ++x
)
5343 memc
->number_of_hosts
= number_of_hosts
;
5345 return TEST_SUCCESS
;
5348 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5350 memcached_return_t rc
;
5351 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5352 test_true(rc
== MEMCACHED_SUCCESS
);
5354 return regression_bug_434843(memc
);
5357 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5359 memcached_return_t rc
;
5360 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5361 test_true(rc
== MEMCACHED_SUCCESS
);
5363 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5364 test_true(rc
== MEMCACHED_SUCCESS
);
5365 test_true(bytes
!= NULL
);
5366 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5368 test_true(rc
== MEMCACHED_SUCCESS
);
5369 test_true(bytes_read
!= NULL
);
5371 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5372 "bytes_written", &rc
);
5373 test_true(rc
== MEMCACHED_SUCCESS
);
5374 test_true(bytes_written
!= NULL
);
5376 test_true(strcmp(bytes
, bytes_read
) != 0);
5377 test_true(strcmp(bytes
, bytes_written
) != 0);
5379 /* Release allocated resources */
5382 free(bytes_written
);
5383 memcached_stat_free(NULL
, memc_stat
);
5385 return TEST_SUCCESS
;
5389 * The test case isn't obvious so I should probably document why
5390 * it works the way it does. Bug 442914 was caused by a bug
5391 * in the logic in memcached_purge (it did not handle the case
5392 * where the number of bytes sent was equal to the watermark).
5393 * In this test case, create messages so that we hit that case
5394 * and then disable noreply mode and issue a new command to
5395 * verify that it isn't stuck. If we change the format for the
5396 * delete command or the watermarks, we need to update this
5399 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5401 memcached_return_t rc
;
5402 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5403 test_true(rc
== MEMCACHED_SUCCESS
);
5404 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5406 uint32_t number_of_hosts
= memcached_server_count(memc
);
5407 memc
->number_of_hosts
= 1;
5412 for (uint32_t x
= 0; x
< 250; ++x
)
5414 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5415 rc
= memcached_delete(memc
, k
, len
, 0);
5416 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5419 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5422 rc
= memcached_delete(memc
, k
, len
, 0);
5423 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5425 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5426 test_true(rc
== MEMCACHED_SUCCESS
);
5427 rc
= memcached_delete(memc
, k
, len
, 0);
5428 test_true(rc
== MEMCACHED_NOTFOUND
);
5430 memc
->number_of_hosts
= number_of_hosts
;
5432 return TEST_SUCCESS
;
5435 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5437 memcached_server_instance_st instance_one
;
5438 memcached_server_instance_st instance_two
;
5440 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5441 return TEST_SKIPPED
;
5443 memcached_return_t rc
;
5445 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5446 test_true(rc
== MEMCACHED_SUCCESS
);
5448 const size_t max_keys
= 100;
5449 char **keys
= calloc(max_keys
, sizeof(char*));
5450 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5452 for (size_t x
= 0; x
< max_keys
; ++x
)
5456 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5458 test_true(keys
[x
] != NULL
);
5459 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5460 test_true(rc
== MEMCACHED_SUCCESS
);
5464 ** We are using the quiet commands to store the replicas, so we need
5465 ** to ensure that all of them are processed before we can continue.
5466 ** In the test we go directly from storing the object to trying to
5467 ** receive the object from all of the different servers, so we
5468 ** could end up in a race condition (the memcached server hasn't yet
5469 ** processed the quiet command from the replication set when it process
5470 ** the request from the other client (created by the clone)). As a
5471 ** workaround for that we call memcached_quit to send the quit command
5472 ** to the server and wait for the response ;-) If you use the test code
5473 ** as an example for your own code, please note that you shouldn't need
5476 memcached_quit(memc
);
5478 /* Verify that all messages are stored, and we didn't stuff too much
5481 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5482 test_true(rc
== MEMCACHED_SUCCESS
);
5485 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5486 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5487 /* Verify that we received all of the key/value pairs */
5488 test_true(counter
== max_keys
);
5490 memcached_quit(memc
);
5492 * Don't do the following in your code. I am abusing the internal details
5493 * within the library, and this is not a supported interface.
5494 * This is to verify correct behavior in the library. Fake that two servers
5497 instance_one
= memcached_server_instance_by_position(memc
, 0);
5498 instance_two
= memcached_server_instance_by_position(memc
, 2);
5499 in_port_t port0
= instance_one
->port
;
5500 in_port_t port2
= instance_two
->port
;
5502 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5503 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5505 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5506 test_true(rc
== MEMCACHED_SUCCESS
);
5509 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5510 test_true(counter
== (unsigned int)max_keys
);
5512 /* restore the memc handle */
5513 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5514 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5516 memcached_quit(memc
);
5518 /* Remove half of the objects */
5519 for (size_t x
= 0; x
< max_keys
; ++x
)
5523 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5524 test_true(rc
== MEMCACHED_SUCCESS
);
5528 memcached_quit(memc
);
5529 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5530 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5532 /* now retry the command, this time we should have cache misses */
5533 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5534 test_true(rc
== MEMCACHED_SUCCESS
);
5537 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5538 test_true(counter
== (unsigned int)(max_keys
>> 1));
5540 /* Release allocated resources */
5541 for (size_t x
= 0; x
< max_keys
; ++x
)
5548 /* restore the memc handle */
5549 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5550 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5552 return TEST_SUCCESS
;
5555 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5557 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5558 test_true(memc_clone
!= NULL
);
5559 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5561 memcached_server_instance_st instance
=
5562 memcached_server_instance_by_position(memc_clone
, 0);
5564 if (instance
->major_version
> 1 ||
5565 (instance
->major_version
== 1 &&
5566 instance
->minor_version
> 2))
5568 /* Binary protocol doesn't support deferred delete */
5569 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5570 test_true(bin_clone
!= NULL
);
5571 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5572 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5573 memcached_free(bin_clone
);
5575 memcached_quit(memc_clone
);
5577 /* If we know the server version, deferred delete should fail
5578 * with invalid arguments */
5579 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5581 /* If we don't know the server version, we should get a protocol error */
5582 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5584 /* but there is a bug in some of the memcached servers (1.4) that treats
5585 * the counter as noreply so it doesn't send the proper error message
5587 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5589 /* And buffered mode should be disabled and we should get protocol error */
5590 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5591 rc
= memcached_delete(memc
, "foo", 3, 1);
5592 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5594 /* Same goes for noreply... */
5595 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5596 rc
= memcached_delete(memc
, "foo", 3, 1);
5597 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5599 /* but a normal request should go through (and be buffered) */
5600 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5601 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5603 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5604 /* unbuffered noreply should be success */
5605 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5606 /* unbuffered with reply should be not found... */
5607 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5608 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5611 memcached_free(memc_clone
);
5612 return TEST_SUCCESS
;
5616 /* Test memcached_server_get_last_disconnect
5617 * For a working server set, shall be NULL
5618 * For a set of non existing server, shall not be NULL
5620 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5622 memcached_return_t rc
;
5623 memcached_server_instance_st disconnected_server
;
5625 /* With the working set of server */
5626 const char *key
= "marmotte";
5627 const char *value
= "milka";
5629 memcached_reset_last_disconnected_server(memc
);
5630 rc
= memcached_set(memc
, key
, strlen(key
),
5631 value
, strlen(value
),
5632 (time_t)0, (uint32_t)0);
5633 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5635 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5636 test_true(disconnected_server
== NULL
);
5638 /* With a non existing server */
5640 memcached_server_st
*servers
;
5642 const char *server_list
= "localhost:9";
5644 servers
= memcached_servers_parse(server_list
);
5646 mine
= memcached_create(NULL
);
5647 rc
= memcached_server_push(mine
, servers
);
5648 test_true(rc
== MEMCACHED_SUCCESS
);
5649 memcached_server_list_free(servers
);
5652 rc
= memcached_set(mine
, key
, strlen(key
),
5653 value
, strlen(value
),
5654 (time_t)0, (uint32_t)0);
5655 test_true(rc
!= MEMCACHED_SUCCESS
);
5657 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5658 if (disconnected_server
== NULL
)
5660 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5663 test_true(disconnected_server
!= NULL
);
5664 test_true(memcached_server_port(disconnected_server
)== 9);
5665 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5667 memcached_quit(mine
);
5668 memcached_free(mine
);
5670 return TEST_SUCCESS
;
5673 static test_return_t
test_verbosity(memcached_st
*memc
)
5675 memcached_verbosity(memc
, 3);
5677 return TEST_SUCCESS
;
5680 static test_return_t
test_server_failure(memcached_st
*memc
)
5682 memcached_st
*local_memc
;
5683 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5685 local_memc
= memcached_create(NULL
);
5687 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5688 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5690 uint32_t server_count
= memcached_server_count(local_memc
);
5692 test_true(server_count
== 1);
5694 // Disable the server
5695 instance
= memcached_server_instance_by_position(local_memc
, 0);
5696 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5698 memcached_return_t rc
;
5699 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5701 (time_t)0, (uint32_t)0);
5702 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5704 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5705 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5707 (time_t)0, (uint32_t)0);
5708 test_true(rc
== MEMCACHED_SUCCESS
);
5711 memcached_free(local_memc
);
5713 return TEST_SUCCESS
;
5716 static test_return_t
test_cull_servers(memcached_st
*memc
)
5718 uint32_t count
= memcached_server_count(memc
);
5720 // Do not do this in your code, it is not supported.
5721 memc
->servers
[1].state
.is_dead
= true;
5722 memc
->state
.is_time_for_rebuild
= true;
5724 uint32_t new_count
= memcached_server_count(memc
);
5725 test_true(count
== new_count
);
5728 test_true(count
== new_count
+ 1 );
5731 return TEST_SUCCESS
;
5735 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5736 const char *key
, size_t key_length
,
5737 const char *value
, size_t value_length
,
5747 return MEMCACHED_SUCCESS
;
5750 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5752 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5753 test_true(rc
== MEMCACHED_SUCCESS
);
5755 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5756 test_true(rc
== MEMCACHED_SUCCESS
);
5758 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5759 test_true(rc
== MEMCACHED_SUCCESS
);
5761 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5762 test_true(rc
== MEMCACHED_SUCCESS
);
5764 return TEST_SUCCESS
;
5768 * This test ensures that the failure counter isn't incremented during
5769 * normal termination of the memcached instance.
5771 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5773 memcached_return_t rc
;
5774 memcached_server_instance_st instance
;
5776 /* Set value to force connection to the server */
5777 const char *key
= "marmotte";
5778 const char *value
= "milka";
5781 * Please note that I'm abusing the internal structures in libmemcached
5782 * in a non-portable way and you shouldn't be doing this. I'm only
5783 * doing this in order to verify that the library works the way it should
5785 uint32_t number_of_hosts
= memcached_server_count(memc
);
5786 memc
->number_of_hosts
= 1;
5788 /* Ensure that we are connected to the server by setting a value */
5789 rc
= memcached_set(memc
, key
, strlen(key
),
5790 value
, strlen(value
),
5791 (time_t)0, (uint32_t)0);
5792 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5795 instance
= memcached_server_instance_by_position(memc
, 0);
5796 /* The test is to see that the memcached_quit doesn't increase the
5797 * the server failure conter, so let's ensure that it is zero
5798 * before sending quit
5800 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5802 memcached_quit(memc
);
5804 /* Verify that it memcached_quit didn't increment the failure counter
5805 * Please note that this isn't bullet proof, because an error could
5808 test_true(instance
->server_failure_counter
== 0);
5810 /* restore the instance */
5811 memc
->number_of_hosts
= number_of_hosts
;
5813 return TEST_SUCCESS
;
5820 * Test that ensures mget_execute does not end into recursive calls that finally fails
5822 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5825 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5826 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5827 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5828 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5831 return TEST_SKIPPED
; // My MAC can't handle this test
5835 * I only want to hit _one_ server so I know the number of requests I'm
5836 * sending in the pipeline.
5838 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5839 memc
->number_of_hosts
= 1;
5840 size_t max_keys
= 20480;
5843 char **keys
= calloc(max_keys
, sizeof(char*));
5844 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5846 /* First add all of the items.. */
5848 char blob
[1024]= { 0 };
5849 memcached_return rc
;
5850 for (size_t x
= 0; x
< max_keys
; ++x
)
5853 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5855 assert(keys
[x
] != NULL
);
5856 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5858 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5860 break; // We are out of business
5863 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5865 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5868 sleep(1);// We will try to sleep
5871 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5873 // We failed to send everything.
5878 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5881 /* Try to get all of them with a large multiget */
5883 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5884 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5885 (size_t)max_keys
, callbacks
, &counter
, 1);
5887 assert(rc
== MEMCACHED_SUCCESS
);
5888 char* the_value
= NULL
;
5889 char the_key
[MEMCACHED_MAX_KEY
];
5890 size_t the_key_length
;
5891 size_t the_value_length
;
5895 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5897 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5903 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5906 assert(rc
== MEMCACHED_END
);
5908 /* Verify that we got all of the items */
5909 assert(counter
== max_keys
);
5912 /* Release all allocated resources */
5913 for (size_t x
= 0; x
< max_keys
; ++x
)
5920 memc
->number_of_hosts
= number_of_hosts
;
5922 return TEST_SUCCESS
;
5925 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5929 memcached_st
*memc
= memcached_create(NULL
);
5931 memcached_server_add(memc
, "10.2.3.4", 11211);
5933 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5934 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5935 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5936 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5937 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5938 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5940 memcached_return_t rc
;
5944 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5946 test_true(rc
== MEMCACHED_TIMEOUT
);
5948 memcached_free(memc
);
5950 return TEST_SUCCESS
;
5953 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5955 fprintf(stderr
, "Iteration #%u: ", it
);
5957 if(error
== MEMCACHED_ERRNO
)
5959 fprintf(stderr
, "system error %d from %s: %s\n",
5960 errno
, what
, strerror(errno
));
5964 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5965 memcached_strerror(mc
, error
));
5969 #define TEST_CONSTANT_CREATION 200
5971 static test_return_t
regression_bug_(memcached_st
*memc
)
5973 const char *remote_server
;
5976 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5978 return TEST_SKIPPED
;
5981 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5983 memcached_st
* mc
= memcached_create(NULL
);
5984 memcached_return rc
;
5986 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5987 if (rc
!= MEMCACHED_SUCCESS
)
5989 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5992 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5993 if (rc
!= MEMCACHED_SUCCESS
)
5995 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5998 rc
= memcached_server_add(mc
, remote_server
, 0);
5999 if (rc
!= MEMCACHED_SUCCESS
)
6001 memcached_die(mc
, rc
, "memcached_server_add", x
);
6004 const char *set_key
= "akey";
6005 const size_t set_key_len
= strlen(set_key
);
6006 const char *set_value
= "a value";
6007 const size_t set_value_len
= strlen(set_value
);
6009 if (rc
== MEMCACHED_SUCCESS
)
6013 size_t get_value_len
;
6015 uint32_t get_value_flags
;
6017 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
6018 &get_value_flags
, &rc
);
6019 if (rc
!= MEMCACHED_SUCCESS
)
6021 memcached_die(mc
, rc
, "memcached_get", x
);
6027 (get_value_len
!= set_value_len
6028 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
6030 fprintf(stderr
, "Values don't match?\n");
6031 rc
= MEMCACHED_FAILURE
;
6037 rc
= memcached_set(mc
,
6038 set_key
, set_key_len
,
6039 set_value
, set_value_len
,
6043 if (rc
!= MEMCACHED_SUCCESS
)
6045 memcached_die(mc
, rc
, "memcached_set", x
);
6052 if (rc
!= MEMCACHED_SUCCESS
)
6058 return TEST_SUCCESS
;
6062 * Test that the sasl authentication works. We cannot use the default
6063 * pool of servers, because that would require that all servers we want
6064 * to test supports SASL authentication, and that they use the default
6067 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6069 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6070 memcached_return_t rc
;
6072 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6073 test_true(rc
== MEMCACHED_SUCCESS
);
6074 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6075 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6076 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6077 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6078 memcached_quit(memc
);
6080 rc
= memcached_set_sasl_auth_data(memc
,
6081 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6082 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6083 test_true(rc
== MEMCACHED_SUCCESS
);
6085 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6086 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6087 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6089 memcached_quit(memc
);
6090 return TEST_SUCCESS
;
6093 return TEST_FAILURE
;
6097 /* Clean the server before beginning testing */
6099 {"util_version", 1, (test_callback_fn
)util_version_test
},
6100 {"flush", 0, (test_callback_fn
)flush_test
},
6101 {"init", 0, (test_callback_fn
)init_test
},
6102 {"allocation", 0, (test_callback_fn
)allocation_test
},
6103 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6104 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6105 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6106 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6107 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6108 {"clone_test", 0, (test_callback_fn
)clone_test
},
6109 {"connection_test", 0, (test_callback_fn
)connection_test
},
6110 {"callback_test", 0, (test_callback_fn
)callback_test
},
6111 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6112 {"error", 0, (test_callback_fn
)error_test
},
6113 {"set", 0, (test_callback_fn
)set_test
},
6114 {"set2", 0, (test_callback_fn
)set_test2
},
6115 {"set3", 0, (test_callback_fn
)set_test3
},
6116 {"dump", 1, (test_callback_fn
)dump_test
},
6117 {"add", 1, (test_callback_fn
)add_test
},
6118 {"replace", 1, (test_callback_fn
)replace_test
},
6119 {"delete", 1, (test_callback_fn
)delete_test
},
6120 {"get", 1, (test_callback_fn
)get_test
},
6121 {"get2", 0, (test_callback_fn
)get_test2
},
6122 {"get3", 0, (test_callback_fn
)get_test3
},
6123 {"get4", 0, (test_callback_fn
)get_test4
},
6124 {"partial mget", 0, (test_callback_fn
)get_test5
},
6125 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6126 {"increment", 0, (test_callback_fn
)increment_test
},
6127 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6128 {"decrement", 0, (test_callback_fn
)decrement_test
},
6129 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6130 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6131 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6132 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6133 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6134 {"quit", 0, (test_callback_fn
)quit_test
},
6135 {"mget", 1, (test_callback_fn
)mget_test
},
6136 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6137 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6138 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6139 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6140 {"mget_end", 0, (test_callback_fn
)mget_end
},
6141 {"get_stats", 0, (test_callback_fn
)get_stats
},
6142 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6143 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6144 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6145 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6146 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6147 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6148 {"read_through", 1, (test_callback_fn
)read_through
},
6149 {"delete_through", 1, (test_callback_fn
)delete_through
},
6150 {"noreply", 1, (test_callback_fn
)noreply_test
},
6151 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6152 #ifdef HAVE_LIBMEMCACHEDUTIL
6153 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6154 {"ping", 1, (test_callback_fn
)ping_test
},
6156 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6157 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6158 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6159 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6160 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6164 test_st behavior_tests
[] ={
6165 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6166 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6167 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6168 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6172 test_st regression_binary_vs_block
[] ={
6173 {"block add", 1, (test_callback_fn
)block_add_regression
},
6174 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6178 test_st async_tests
[] ={
6179 {"add", 1, (test_callback_fn
)add_wrapper
},
6183 test_st string_tests
[] ={
6184 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6185 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6186 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6187 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6188 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6189 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6190 {0, 0, (test_callback_fn
)0}
6193 test_st result_tests
[] ={
6194 {"result static", 0, (test_callback_fn
)result_static
},
6195 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6196 {0, 0, (test_callback_fn
)0}
6199 test_st version_1_2_3
[] ={
6200 {"append", 0, (test_callback_fn
)append_test
},
6201 {"prepend", 0, (test_callback_fn
)prepend_test
},
6202 {"cas", 0, (test_callback_fn
)cas_test
},
6203 {"cas2", 0, (test_callback_fn
)cas2_test
},
6204 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6205 {0, 0, (test_callback_fn
)0}
6208 test_st user_tests
[] ={
6209 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6210 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6211 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6212 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6213 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6214 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6215 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6216 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6217 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6218 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6219 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6220 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6221 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6222 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6223 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6224 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6227 ** It seems to be something weird with the character sets..
6228 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6229 ** guess I need to find out how this is supposed to work.. Perhaps I need
6230 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6231 ** so just disable the code for now...).
6233 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6235 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6236 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6237 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6238 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6239 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6240 {0, 0, (test_callback_fn
)0}
6243 test_st replication_tests
[]= {
6244 {"set", 1, (test_callback_fn
)replication_set_test
},
6245 {"get", 0, (test_callback_fn
)replication_get_test
},
6246 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6247 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6248 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6249 {0, 0, (test_callback_fn
)0}
6253 * The following test suite is used to verify that we don't introduce
6254 * regression bugs. If you want more information about the bug / test,
6255 * you should look in the bug report at
6256 * http://bugs.launchpad.net/libmemcached
6258 test_st regression_tests
[]= {
6259 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6260 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6261 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6262 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6263 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6264 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6265 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6266 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6267 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6268 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6269 {0, 0, (test_callback_fn
)0}
6272 test_st sasl_auth_tests
[]= {
6273 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6274 {0, 0, (test_callback_fn
)0}
6277 test_st ketama_compatibility
[]= {
6278 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6279 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6280 {0, 0, (test_callback_fn
)0}
6283 test_st generate_tests
[] ={
6284 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6285 {"generate_data", 1, (test_callback_fn
)generate_data
},
6286 {"get_read", 0, (test_callback_fn
)get_read
},
6287 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6288 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6289 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6290 {"generate_data", 1, (test_callback_fn
)generate_data
},
6291 {"mget_read", 0, (test_callback_fn
)mget_read
},
6292 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6293 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6294 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6295 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6296 {"generate_data", 1, (test_callback_fn
)generate_data
},
6297 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6298 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6299 {0, 0, (test_callback_fn
)0}
6302 test_st consistent_tests
[] ={
6303 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6304 {"generate_data", 1, (test_callback_fn
)generate_data
},
6305 {"get_read", 0, (test_callback_fn
)get_read_count
},
6306 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6307 {0, 0, (test_callback_fn
)0}
6310 test_st consistent_weighted_tests
[] ={
6311 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6312 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6313 {"get_read", 0, (test_callback_fn
)get_read_count
},
6314 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6315 {0, 0, (test_callback_fn
)0}
6318 test_st hsieh_availability
[] ={
6319 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6320 {0, 0, (test_callback_fn
)0}
6324 test_st hash_sanity
[] ={
6325 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6326 {0, 0, (test_callback_fn
)0}
6330 test_st ketama_auto_eject_hosts
[] ={
6331 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6332 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6333 {0, 0, (test_callback_fn
)0}
6336 test_st hash_tests
[] ={
6337 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6338 {"md5", 0, (test_callback_fn
)md5_run
},
6339 {"crc", 0, (test_callback_fn
)crc_run
},
6340 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6341 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6342 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6343 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6344 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6345 {"murmur", 0, (test_callback_fn
)murmur_run
},
6346 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6347 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6348 {0, 0, (test_callback_fn
)0}
6351 test_st error_conditions
[] ={
6352 {"memcached_get_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6353 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6354 {"memcached_get_by_key_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6355 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6356 {0, 0, (test_callback_fn
)0}
6359 collection_st collection
[] ={
6361 {"hash_sanity", 0, 0, hash_sanity
},
6363 {"hsieh_availability", 0, 0, hsieh_availability
},
6364 {"block", 0, 0, tests
},
6365 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6366 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6367 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6368 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6369 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6370 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6371 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6372 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6373 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6374 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6375 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6376 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6377 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6378 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6379 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6380 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6381 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6382 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6383 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6384 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6385 #ifdef MEMCACHED_ENABLE_DEPRECATED
6386 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6388 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6389 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6390 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6391 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6392 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6393 {"string", 0, 0, string_tests
},
6394 {"result", 0, 0, result_tests
},
6395 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6396 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6397 {"user", 0, 0, user_tests
},
6398 {"generate", 0, 0, generate_tests
},
6399 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6400 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6401 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6402 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6403 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6404 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6405 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6407 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6408 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6409 {"consistent_not", 0, 0, consistent_tests
},
6410 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6411 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6412 {"ketama_compat", 0, 0, ketama_compatibility
},
6413 {"test_hashes", 0, 0, hash_tests
},
6414 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6415 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6416 {"regression", 0, 0, regression_tests
},
6417 {"behaviors", 0, 0, behavior_tests
},
6418 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6419 {"error_conditions", 0, 0, error_conditions
},
6423 #define SERVERS_TO_CREATE 5
6425 #include "libmemcached_world.h"
6427 void get_world(world_st
*world
)
6429 world
->collections
= collection
;
6431 world
->create
= (test_callback_create_fn
)world_create
;
6432 world
->destroy
= (test_callback_fn
)world_destroy
;
6434 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6435 world
->test
.flush
= (test_callback_fn
)world_flush
;
6436 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6437 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6438 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6440 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6441 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6443 world
->runner
= &defualt_libmemcached_runner
;