1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
13 #include "libmemcached/common.h"
20 #include <sys/types.h>
26 #include "clients/generator.h"
27 #include "clients/execute.h"
30 #define INT64_MAX LONG_MAX
33 #define INT32_MAX INT_MAX
39 #ifdef HAVE_LIBMEMCACHEDUTIL
41 #include "libmemcached/memcached_util.h"
44 #include "hash_results.h"
46 #define GLOBAL_COUNT 10000
47 #define GLOBAL2_COUNT 100
48 #define SERVERS_TO_CREATE 5
49 static uint32_t global_count
;
51 static pairs_st
*global_pairs
;
52 static const char *global_keys
[GLOBAL_COUNT
];
53 static size_t global_keys_length
[GLOBAL_COUNT
];
56 static test_return_t
pre_binary(memcached_st
*memc
);
59 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
63 (void)memcached_create(&memc
);
64 memcached_free(&memc
);
69 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
71 memcached_server_st
*server_list
;
72 memcached_return_t rc
;
74 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
75 test_true(server_list
== NULL
);
77 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
78 test_true(server_list
== NULL
);
80 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
81 test_true(server_list
== NULL
);
86 #define TEST_PORT_COUNT 7
87 in_port_t test_ports
[TEST_PORT_COUNT
];
89 static memcached_return_t
server_display_function(const memcached_st
*ptr
__attribute__((unused
)),
90 const memcached_server_st
*server
,
94 size_t bigger
= *((size_t *)(context
));
95 assert(bigger
<= memcached_server_port(server
));
96 *((size_t *)(context
))= memcached_server_port(server
);
98 return MEMCACHED_SUCCESS
;
101 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
103 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
105 memcached_return_t rc
;
106 memcached_server_fn callbacks
[1];
107 memcached_st
*local_memc
;
109 local_memc
= memcached_create(NULL
);
110 test_true(local_memc
);
111 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
113 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
115 test_ports
[x
]= (in_port_t
)random() % 64000;
116 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
117 test_true(memcached_server_count(local_memc
) == x
+ 1);
119 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
121 test_true(rc
== MEMCACHED_SUCCESS
);
124 callbacks
[0]= server_display_function
;
125 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
128 memcached_free(local_memc
);
133 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
135 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
136 memcached_return_t rc
;
137 memcached_server_fn callbacks
[1];
138 memcached_st
*local_memc
;
139 memcached_server_instance_st instance
;
141 local_memc
= memcached_create(NULL
);
142 test_true(local_memc
);
143 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
144 test_true(rc
== MEMCACHED_SUCCESS
);
146 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
147 test_true(rc
== MEMCACHED_SUCCESS
);
148 instance
= memcached_server_instance_by_position(local_memc
, 0);
149 test_true(memcached_server_port(instance
) == 43043);
151 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
152 test_true(rc
== MEMCACHED_SUCCESS
);
154 instance
= memcached_server_instance_by_position(local_memc
, 0);
155 test_true(memcached_server_port(instance
) == 43042);
157 instance
= memcached_server_instance_by_position(local_memc
, 1);
158 test_true(memcached_server_port(instance
) == 43043);
160 callbacks
[0]= server_display_function
;
161 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
164 memcached_free(local_memc
);
169 static memcached_return_t
server_print_callback(const memcached_st
*ptr
__attribute__((unused
)),
170 const memcached_server_st
*server
,
171 void *context
__attribute__((unused
)))
173 (void)server
; // Just in case we aren't printing.
176 fprintf(stderr
, "%s(%d)", memcached_server_name(server
), memcached_server_port(server
));
179 return MEMCACHED_SUCCESS
;
182 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
__attribute__((unused
)))
184 memcached_return_t rc
;
185 memcached_st local_memc
;
187 memcached_server_st
*servers
;
188 memcached_server_fn callbacks
[1];
190 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";
192 memc
= memcached_create(&local_memc
);
194 servers
= memcached_servers_parse(server_string
);
196 rc
= memcached_server_push(memc
, servers
);
197 memcached_server_list_free(servers
);
199 callbacks
[0]= server_print_callback
;
200 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
202 memcached_free(memc
);
207 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
__attribute__((unused
)),
208 const memcached_server_st
*server
,
212 uint32_t x
= *((uint32_t *)(context
));
214 assert(test_ports
[x
] == server
->port
);
215 *((uint32_t *)(context
))= ++x
;
217 return MEMCACHED_SUCCESS
;
220 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
222 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
223 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
224 memcached_return_t rc
;
225 memcached_server_fn callbacks
[1];
226 memcached_st
*local_memc
;
228 local_memc
= memcached_create(NULL
);
229 test_true(local_memc
);
231 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
233 test_ports
[x
]= (in_port_t
)(random() % 64000);
234 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
235 test_true(memcached_server_count(local_memc
) == x
+1);
237 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
239 test_true(rc
== MEMCACHED_SUCCESS
);
242 callbacks
[0]= server_display_unsort_function
;
243 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
245 /* Now we sort old data! */
246 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
247 callbacks
[0]= server_display_function
;
248 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
251 memcached_free(local_memc
);
256 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
259 memc
= memcached_create(NULL
);
261 memcached_free(memc
);
266 static test_return_t
clone_test(memcached_st
*memc
)
270 memcached_st
*memc_clone
;
271 memc_clone
= memcached_clone(NULL
, NULL
);
272 test_true(memc_clone
);
273 memcached_free(memc_clone
);
276 /* Can we init from null? */
278 memcached_st
*memc_clone
;
279 memc_clone
= memcached_clone(NULL
, memc
);
280 test_true(memc_clone
);
283 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
284 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
285 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
286 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
289 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
290 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
291 test_true(memc_clone
->distribution
== memc
->distribution
);
292 { // Test all of the flags
293 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
294 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
295 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
296 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
297 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
298 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
299 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
300 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
301 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
302 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
303 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
304 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
305 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
306 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
307 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
309 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
310 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
311 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
312 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
313 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
314 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
315 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
316 test_true(memc_clone
->on_clone
== memc
->on_clone
);
317 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
318 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
319 test_true(memc_clone
->recv_size
== memc
->recv_size
);
320 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
321 test_true(memc_clone
->send_size
== memc
->send_size
);
322 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
323 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
324 test_true(memc_clone
->user_data
== memc
->user_data
);
326 memcached_free(memc_clone
);
329 /* Can we init from struct? */
331 memcached_st declared_clone
;
332 memcached_st
*memc_clone
;
333 memset(&declared_clone
, 0 , sizeof(memcached_st
));
334 memc_clone
= memcached_clone(&declared_clone
, NULL
);
335 test_true(memc_clone
);
336 memcached_free(memc_clone
);
339 /* Can we init from struct? */
341 memcached_st declared_clone
;
342 memcached_st
*memc_clone
;
343 memset(&declared_clone
, 0 , sizeof(memcached_st
));
344 memc_clone
= memcached_clone(&declared_clone
, memc
);
345 test_true(memc_clone
);
346 memcached_free(memc_clone
);
352 static test_return_t
userdata_test(memcached_st
*memc
)
355 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
356 test_true(memcached_get_user_data(memc
) == foo
);
357 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
362 static test_return_t
connection_test(memcached_st
*memc
)
364 memcached_return_t rc
;
366 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
367 test_true(rc
== MEMCACHED_SUCCESS
);
372 static test_return_t
error_test(memcached_st
*memc
)
374 memcached_return_t rc
;
375 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
376 982370485U, 1263635348U, 4242906218U, 3829656100U,
377 1891735253U, 334139633U, 2257084983U, 3088286104U,
378 13199785U, 2542027183U, 1097051614U, 199566778U,
379 2748246961U, 2465192557U, 1664094137U, 2405439045U,
380 1842224848U, 692413798U, 3479807801U, 919913813U,
381 4269430871U, 610793021U, 527273862U, 1437122909U,
382 2300930706U, 2943759320U, 674306647U, 2400528935U,
383 54481931U, 4186304426U, 1741088401U, 2979625118U,
384 4159057246U, 3425930182U, 2593724503U, 1868899624U,
385 1769812374U, 2302537950U, 1110330676U };
387 // You have updated the memcache_error messages but not updated docs/tests.
388 test_true(MEMCACHED_MAXIMUM_RETURN
== 43);
389 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
392 const char *msg
= memcached_strerror(memc
, rc
);
393 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
394 MEMCACHED_HASH_JENKINS
);
395 if (values
[rc
] != hash_val
)
397 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
398 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
400 test_true(values
[rc
] == hash_val
);
406 static test_return_t
set_test(memcached_st
*memc
)
408 memcached_return_t rc
;
409 const char *key
= "foo";
410 const char *value
= "when we sanitize";
412 rc
= memcached_set(memc
, key
, strlen(key
),
413 value
, strlen(value
),
414 (time_t)0, (uint32_t)0);
415 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
420 static test_return_t
append_test(memcached_st
*memc
)
422 memcached_return_t rc
;
423 const char *key
= "fig";
424 const char *in_value
= "we";
425 char *out_value
= NULL
;
429 rc
= memcached_flush(memc
, 0);
430 test_true(rc
== MEMCACHED_SUCCESS
);
432 rc
= memcached_set(memc
, key
, strlen(key
),
433 in_value
, strlen(in_value
),
434 (time_t)0, (uint32_t)0);
435 test_true(rc
== MEMCACHED_SUCCESS
);
437 rc
= memcached_append(memc
, key
, strlen(key
),
438 " the", strlen(" the"),
439 (time_t)0, (uint32_t)0);
440 test_true(rc
== MEMCACHED_SUCCESS
);
442 rc
= memcached_append(memc
, key
, strlen(key
),
443 " people", strlen(" people"),
444 (time_t)0, (uint32_t)0);
445 test_true(rc
== MEMCACHED_SUCCESS
);
447 out_value
= memcached_get(memc
, key
, strlen(key
),
448 &value_length
, &flags
, &rc
);
449 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
450 test_true(strlen("we the people") == value_length
);
451 test_true(rc
== MEMCACHED_SUCCESS
);
457 static test_return_t
append_binary_test(memcached_st
*memc
)
459 memcached_return_t rc
;
460 const char *key
= "numbers";
461 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
467 rc
= memcached_flush(memc
, 0);
468 test_true(rc
== MEMCACHED_SUCCESS
);
470 rc
= memcached_set(memc
,
473 (time_t)0, (uint32_t)0);
474 test_true(rc
== MEMCACHED_SUCCESS
);
476 for (x
= 0; store_list
[x
] ; x
++)
478 rc
= memcached_append(memc
,
480 (char *)&store_list
[x
], sizeof(uint32_t),
481 (time_t)0, (uint32_t)0);
482 test_true(rc
== MEMCACHED_SUCCESS
);
485 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
486 &value_length
, &flags
, &rc
);
487 test_true((value_length
== (sizeof(uint32_t) * x
)));
488 test_true(rc
== MEMCACHED_SUCCESS
);
490 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
492 test_true(*ptr
== store_list
[x
- counter
]);
500 static test_return_t
cas2_test(memcached_st
*memc
)
502 memcached_return_t rc
;
503 const char *keys
[]= {"fudge", "son", "food"};
504 size_t key_length
[]= {5, 3, 4};
505 const char *value
= "we the people";
506 size_t value_length
= strlen("we the people");
508 memcached_result_st results_obj
;
509 memcached_result_st
*results
;
512 rc
= memcached_flush(memc
, 0);
513 test_true(rc
== MEMCACHED_SUCCESS
);
515 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
517 for (x
= 0; x
< 3; x
++)
519 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
520 keys
[x
], key_length
[x
],
521 (time_t)50, (uint32_t)9);
522 test_true(rc
== MEMCACHED_SUCCESS
);
525 rc
= memcached_mget(memc
, keys
, key_length
, 3);
527 results
= memcached_result_create(memc
, &results_obj
);
529 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
531 test_true(results
->item_cas
);
532 test_true(rc
== MEMCACHED_SUCCESS
);
533 test_true(memcached_result_cas(results
));
535 test_true(!memcmp(value
, "we the people", strlen("we the people")));
536 test_true(strlen("we the people") == value_length
);
537 test_true(rc
== MEMCACHED_SUCCESS
);
539 memcached_result_free(&results_obj
);
544 static test_return_t
cas_test(memcached_st
*memc
)
546 memcached_return_t rc
;
547 const char *key
= "fun";
548 size_t key_length
= strlen(key
);
549 const char *value
= "we the people";
550 const char* keys
[2] = { key
, NULL
};
551 size_t keylengths
[2] = { strlen(key
), 0 };
552 size_t value_length
= strlen(value
);
553 const char *value2
= "change the value";
554 size_t value2_length
= strlen(value2
);
556 memcached_result_st results_obj
;
557 memcached_result_st
*results
;
560 rc
= memcached_flush(memc
, 0);
561 test_true(rc
== MEMCACHED_SUCCESS
);
563 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
565 rc
= memcached_set(memc
, key
, strlen(key
),
566 value
, strlen(value
),
567 (time_t)0, (uint32_t)0);
568 test_true(rc
== MEMCACHED_SUCCESS
);
570 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
572 results
= memcached_result_create(memc
, &results_obj
);
574 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
576 test_true(rc
== MEMCACHED_SUCCESS
);
577 test_true(memcached_result_cas(results
));
578 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
579 test_true(strlen(memcached_result_value(results
)) == value_length
);
580 test_true(rc
== MEMCACHED_SUCCESS
);
581 uint64_t cas
= memcached_result_cas(results
);
584 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
585 test_true(rc
== MEMCACHED_END
);
586 test_true(results
== NULL
);
589 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
590 test_true(rc
== MEMCACHED_SUCCESS
);
593 * The item will have a new cas value, so try to set it again with the old
594 * value. This should fail!
596 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
597 test_true(rc
== MEMCACHED_DATA_EXISTS
);
599 memcached_result_free(&results_obj
);
604 static test_return_t
prepend_test(memcached_st
*memc
)
606 memcached_return_t rc
;
607 const char *key
= "fig";
608 const char *value
= "people";
609 char *out_value
= NULL
;
613 rc
= memcached_flush(memc
, 0);
614 test_true(rc
== MEMCACHED_SUCCESS
);
616 rc
= memcached_set(memc
, key
, strlen(key
),
617 value
, strlen(value
),
618 (time_t)0, (uint32_t)0);
619 test_true(rc
== MEMCACHED_SUCCESS
);
621 rc
= memcached_prepend(memc
, key
, strlen(key
),
622 "the ", strlen("the "),
623 (time_t)0, (uint32_t)0);
624 test_true(rc
== MEMCACHED_SUCCESS
);
626 rc
= memcached_prepend(memc
, key
, strlen(key
),
627 "we ", strlen("we "),
628 (time_t)0, (uint32_t)0);
629 test_true(rc
== MEMCACHED_SUCCESS
);
631 out_value
= memcached_get(memc
, key
, strlen(key
),
632 &value_length
, &flags
, &rc
);
633 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
634 test_true(strlen("we the people") == value_length
);
635 test_true(rc
== MEMCACHED_SUCCESS
);
642 Set the value, then quit to make sure it is flushed.
643 Come back in and test that add fails.
645 static test_return_t
add_test(memcached_st
*memc
)
647 memcached_return_t rc
;
648 const char *key
= "foo";
649 const char *value
= "when we sanitize";
650 unsigned long long setting_value
;
652 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
654 rc
= memcached_set(memc
, key
, strlen(key
),
655 value
, strlen(value
),
656 (time_t)0, (uint32_t)0);
657 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
658 memcached_quit(memc
);
659 rc
= memcached_add(memc
, key
, strlen(key
),
660 value
, strlen(value
),
661 (time_t)0, (uint32_t)0);
663 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
666 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
670 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
677 ** There was a problem of leaking filedescriptors in the initial release
678 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
679 ** systems it seems that the kernel is slow on reclaiming the resources
680 ** because the connects starts to time out (the test doesn't do much
681 ** anyway, so just loop 10 iterations)
683 static test_return_t
add_wrapper(memcached_st
*memc
)
686 unsigned int max
= 10000;
694 for (x
= 0; x
< max
; x
++)
700 static test_return_t
replace_test(memcached_st
*memc
)
702 memcached_return_t rc
;
703 const char *key
= "foo";
704 const char *value
= "when we sanitize";
705 const char *original
= "first we insert some data";
707 rc
= memcached_set(memc
, key
, strlen(key
),
708 original
, strlen(original
),
709 (time_t)0, (uint32_t)0);
710 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
712 rc
= memcached_replace(memc
, key
, strlen(key
),
713 value
, strlen(value
),
714 (time_t)0, (uint32_t)0);
715 test_true(rc
== MEMCACHED_SUCCESS
);
720 static test_return_t
delete_test(memcached_st
*memc
)
722 memcached_return_t rc
;
723 const char *key
= "foo";
724 const char *value
= "when we sanitize";
726 rc
= memcached_set(memc
, key
, strlen(key
),
727 value
, strlen(value
),
728 (time_t)0, (uint32_t)0);
729 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
731 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
732 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
737 static test_return_t
flush_test(memcached_st
*memc
)
739 memcached_return_t rc
;
741 rc
= memcached_flush(memc
, 0);
742 test_true(rc
== MEMCACHED_SUCCESS
);
747 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
748 const memcached_server_st
*server
__attribute__((unused
)),
749 void *context
__attribute__((unused
)))
753 return MEMCACHED_SUCCESS
;
756 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
759 strcpy(context
, "foo bad");
760 memcached_server_fn callbacks
[1];
762 callbacks
[0]= server_function
;
763 memcached_server_cursor(memc
, callbacks
, context
, 1);
767 static test_return_t
bad_key_test(memcached_st
*memc
)
769 memcached_return_t rc
;
770 const char *key
= "foo bad";
772 size_t string_length
;
774 memcached_st
*memc_clone
;
776 size_t max_keylen
= 0xffff;
778 // Just skip if we are in binary mode.
779 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
782 memc_clone
= memcached_clone(NULL
, memc
);
783 test_true(memc_clone
);
785 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
786 test_true(rc
== MEMCACHED_SUCCESS
);
788 /* All keys are valid in the binary protocol (except for length) */
789 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
791 string
= memcached_get(memc_clone
, key
, strlen(key
),
792 &string_length
, &flags
, &rc
);
793 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
794 test_true(string_length
== 0);
798 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
799 test_true(rc
== MEMCACHED_SUCCESS
);
800 string
= memcached_get(memc_clone
, key
, strlen(key
),
801 &string_length
, &flags
, &rc
);
802 test_true(rc
== MEMCACHED_NOTFOUND
);
803 test_true(string_length
== 0);
806 /* Test multi key for bad keys */
807 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
808 size_t key_lengths
[] = { 7, 7, 7 };
810 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
811 test_true(rc
== MEMCACHED_SUCCESS
);
813 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
814 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
816 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
817 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
821 /* The following test should be moved to the end of this function when the
822 memcached server is updated to allow max size length of the keys in the
825 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
826 test_true(rc
== MEMCACHED_SUCCESS
);
828 char *longkey
= malloc(max_keylen
+ 1);
831 memset(longkey
, 'a', max_keylen
+ 1);
832 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
833 &string_length
, &flags
, &rc
);
834 test_true(rc
== MEMCACHED_NOTFOUND
);
835 test_true(string_length
== 0);
838 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
839 &string_length
, &flags
, &rc
);
840 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
841 test_true(string_length
== 0);
848 /* Make sure zero length keys are marked as bad */
850 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
851 test_true(rc
== MEMCACHED_SUCCESS
);
852 string
= memcached_get(memc_clone
, key
, 0,
853 &string_length
, &flags
, &rc
);
854 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
855 test_true(string_length
== 0);
858 memcached_free(memc_clone
);
863 #define READ_THROUGH_VALUE "set for me"
864 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
865 char *key
__attribute__((unused
)),
866 size_t key_length
__attribute__((unused
)),
867 memcached_result_st
*result
)
870 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
873 static test_return_t
read_through(memcached_st
*memc
)
875 memcached_return_t rc
;
876 const char *key
= "foo";
878 size_t string_length
;
880 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
882 string
= memcached_get(memc
, key
, strlen(key
),
883 &string_length
, &flags
, &rc
);
885 test_true(rc
== MEMCACHED_NOTFOUND
);
886 test_false(string_length
);
889 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
891 test_true(rc
== MEMCACHED_SUCCESS
);
893 string
= memcached_get(memc
, key
, strlen(key
),
894 &string_length
, &flags
, &rc
);
896 test_true(rc
== MEMCACHED_SUCCESS
);
897 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
898 test_strcmp(READ_THROUGH_VALUE
, string
);
901 string
= memcached_get(memc
, key
, strlen(key
),
902 &string_length
, &flags
, &rc
);
904 test_true(rc
== MEMCACHED_SUCCESS
);
905 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
906 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
912 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
914 size_t key_length
__attribute__((unused
)))
918 return MEMCACHED_SUCCESS
;
921 static test_return_t
delete_through(memcached_st
*memc
)
923 memcached_trigger_delete_key_fn callback
;
924 memcached_return_t rc
;
926 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
928 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
929 test_true(rc
== MEMCACHED_SUCCESS
);
934 static test_return_t
get_test(memcached_st
*memc
)
936 memcached_return_t rc
;
937 const char *key
= "foo";
939 size_t string_length
;
942 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
943 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
945 string
= memcached_get(memc
, key
, strlen(key
),
946 &string_length
, &flags
, &rc
);
948 test_true(rc
== MEMCACHED_NOTFOUND
);
949 test_false(string_length
);
955 static test_return_t
get_test2(memcached_st
*memc
)
957 memcached_return_t rc
;
958 const char *key
= "foo";
959 const char *value
= "when we sanitize";
961 size_t string_length
;
964 rc
= memcached_set(memc
, key
, strlen(key
),
965 value
, strlen(value
),
966 (time_t)0, (uint32_t)0);
967 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
969 string
= memcached_get(memc
, key
, strlen(key
),
970 &string_length
, &flags
, &rc
);
973 test_true(rc
== MEMCACHED_SUCCESS
);
974 test_true(string_length
== strlen(value
));
975 test_true(!memcmp(string
, value
, string_length
));
982 static test_return_t
set_test2(memcached_st
*memc
)
984 memcached_return_t rc
;
985 const char *key
= "foo";
986 const char *value
= "train in the brain";
987 size_t value_length
= strlen(value
);
990 for (x
= 0; x
< 10; x
++)
992 rc
= memcached_set(memc
, key
, strlen(key
),
994 (time_t)0, (uint32_t)0);
995 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1001 static test_return_t
set_test3(memcached_st
*memc
)
1003 memcached_return_t rc
;
1005 size_t value_length
= 8191;
1008 value
= (char*)malloc(value_length
);
1011 for (x
= 0; x
< value_length
; x
++)
1012 value
[x
] = (char) (x
% 127);
1014 /* The dump test relies on there being at least 32 items in memcached */
1015 for (x
= 0; x
< 32; x
++)
1019 sprintf(key
, "foo%u", x
);
1021 rc
= memcached_set(memc
, key
, strlen(key
),
1022 value
, value_length
,
1023 (time_t)0, (uint32_t)0);
1024 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1029 return TEST_SUCCESS
;
1032 static test_return_t
get_test3(memcached_st
*memc
)
1034 memcached_return_t rc
;
1035 const char *key
= "foo";
1037 size_t value_length
= 8191;
1039 size_t string_length
;
1043 value
= (char*)malloc(value_length
);
1046 for (x
= 0; x
< value_length
; x
++)
1047 value
[x
] = (char) (x
% 127);
1049 rc
= memcached_set(memc
, key
, strlen(key
),
1050 value
, value_length
,
1051 (time_t)0, (uint32_t)0);
1052 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1054 string
= memcached_get(memc
, key
, strlen(key
),
1055 &string_length
, &flags
, &rc
);
1057 test_true(rc
== MEMCACHED_SUCCESS
);
1059 test_true(string_length
== value_length
);
1060 test_true(!memcmp(string
, value
, string_length
));
1065 return TEST_SUCCESS
;
1068 static test_return_t
get_test4(memcached_st
*memc
)
1070 memcached_return_t rc
;
1071 const char *key
= "foo";
1073 size_t value_length
= 8191;
1075 size_t string_length
;
1079 value
= (char*)malloc(value_length
);
1082 for (x
= 0; x
< value_length
; x
++)
1083 value
[x
] = (char) (x
% 127);
1085 rc
= memcached_set(memc
, key
, strlen(key
),
1086 value
, value_length
,
1087 (time_t)0, (uint32_t)0);
1088 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1090 for (x
= 0; x
< 10; x
++)
1092 string
= memcached_get(memc
, key
, strlen(key
),
1093 &string_length
, &flags
, &rc
);
1095 test_true(rc
== MEMCACHED_SUCCESS
);
1097 test_true(string_length
== value_length
);
1098 test_true(!memcmp(string
, value
, string_length
));
1104 return TEST_SUCCESS
;
1108 * This test verifies that memcached_read_one_response doesn't try to
1109 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1110 * responses before you execute a storage command.
1112 static test_return_t
get_test5(memcached_st
*memc
)
1115 ** Request the same key twice, to ensure that we hash to the same server
1116 ** (so that we have multiple response values queued up) ;-)
1118 const char *keys
[]= { "key", "key" };
1119 size_t lengths
[]= { 3, 3 };
1123 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1124 keys
[0], lengths
[0], 0, 0);
1125 test_true(rc
== MEMCACHED_SUCCESS
);
1126 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1128 memcached_result_st results_obj
;
1129 memcached_result_st
*results
;
1130 results
=memcached_result_create(memc
, &results_obj
);
1132 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1134 memcached_result_free(&results_obj
);
1136 /* Don't read out the second result, but issue a set instead.. */
1137 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1138 test_true(rc
== MEMCACHED_SUCCESS
);
1140 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1141 &rlen
, &flags
, &rc
);
1142 test_true(val
== NULL
);
1143 test_true(rc
== MEMCACHED_NOTFOUND
);
1144 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1145 test_true(val
!= NULL
);
1146 test_true(rc
== MEMCACHED_SUCCESS
);
1149 return TEST_SUCCESS
;
1152 static test_return_t
mget_end(memcached_st
*memc
)
1154 const char *keys
[]= { "foo", "foo2" };
1155 size_t lengths
[]= { 3, 4 };
1156 const char *values
[]= { "fjord", "41" };
1158 memcached_return_t rc
;
1161 for (int i
= 0; i
< 2; i
++)
1163 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1164 (time_t)0, (uint32_t)0);
1165 test_true(rc
== MEMCACHED_SUCCESS
);
1169 size_t string_length
;
1172 // retrieve both via mget
1173 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1174 test_true(rc
== MEMCACHED_SUCCESS
);
1176 char key
[MEMCACHED_MAX_KEY
];
1179 // this should get both
1180 for (int i
= 0; i
< 2; i
++)
1182 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1184 test_true(rc
== MEMCACHED_SUCCESS
);
1186 if (key_length
== 4)
1188 test_true(string_length
== strlen(values
[val
]));
1189 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1193 // this should indicate end
1194 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1195 test_true(rc
== MEMCACHED_END
);
1198 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1199 test_true(rc
== MEMCACHED_SUCCESS
);
1201 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1202 test_true(key_length
== lengths
[0]);
1203 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1204 test_true(string_length
== strlen(values
[0]));
1205 test_true(strncmp(values
[0], string
, string_length
) == 0);
1206 test_true(rc
== MEMCACHED_SUCCESS
);
1209 // this should indicate end
1210 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1211 test_true(rc
== MEMCACHED_END
);
1213 return TEST_SUCCESS
;
1216 /* Do not copy the style of this code, I just access hosts to testthis function */
1217 static test_return_t
stats_servername_test(memcached_st
*memc
)
1219 memcached_return_t rc
;
1220 memcached_stat_st memc_stat
;
1221 memcached_server_instance_st instance
=
1222 memcached_server_instance_by_position(memc
, 0);
1224 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1225 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1226 return TEST_SKIPPED
;
1228 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1229 memcached_server_name(instance
),
1230 memcached_server_port(instance
));
1232 return TEST_SUCCESS
;
1235 static test_return_t
increment_test(memcached_st
*memc
)
1237 uint64_t new_number
;
1238 memcached_return_t rc
;
1239 const char *key
= "number";
1240 const char *value
= "0";
1242 rc
= memcached_set(memc
, key
, strlen(key
),
1243 value
, strlen(value
),
1244 (time_t)0, (uint32_t)0);
1245 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1247 rc
= memcached_increment(memc
, key
, strlen(key
),
1249 test_true(rc
== MEMCACHED_SUCCESS
);
1250 test_true(new_number
== 1);
1252 rc
= memcached_increment(memc
, key
, strlen(key
),
1254 test_true(rc
== MEMCACHED_SUCCESS
);
1255 test_true(new_number
== 2);
1257 return TEST_SUCCESS
;
1260 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1262 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1264 uint64_t new_number
;
1265 memcached_return_t rc
;
1266 const char *key
= "number";
1267 uint64_t initial
= 0;
1269 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1270 1, initial
, 0, &new_number
);
1271 test_true(rc
== MEMCACHED_SUCCESS
);
1272 test_true(new_number
== initial
);
1274 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1275 1, initial
, 0, &new_number
);
1276 test_true(rc
== MEMCACHED_SUCCESS
);
1277 test_true(new_number
== (initial
+ 1));
1279 return TEST_SUCCESS
;
1282 static test_return_t
decrement_test(memcached_st
*memc
)
1284 uint64_t new_number
;
1285 memcached_return_t rc
;
1286 const char *key
= "number";
1287 const char *value
= "3";
1289 rc
= memcached_set(memc
, key
, strlen(key
),
1290 value
, strlen(value
),
1291 (time_t)0, (uint32_t)0);
1292 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1294 rc
= memcached_decrement(memc
, key
, strlen(key
),
1296 test_true(rc
== MEMCACHED_SUCCESS
);
1297 test_true(new_number
== 2);
1299 rc
= memcached_decrement(memc
, key
, strlen(key
),
1301 test_true(rc
== MEMCACHED_SUCCESS
);
1302 test_true(new_number
== 1);
1304 return TEST_SUCCESS
;
1307 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1309 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1311 uint64_t new_number
;
1312 memcached_return_t rc
;
1313 const char *key
= "number";
1314 uint64_t initial
= 3;
1316 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1317 1, initial
, 0, &new_number
);
1318 test_true(rc
== MEMCACHED_SUCCESS
);
1319 test_true(new_number
== initial
);
1321 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1322 1, initial
, 0, &new_number
);
1323 test_true(rc
== MEMCACHED_SUCCESS
);
1324 test_true(new_number
== (initial
- 1));
1326 return TEST_SUCCESS
;
1329 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1331 uint64_t new_number
;
1332 memcached_return_t rc
;
1333 const char *master_key
= "foo";
1334 const char *key
= "number";
1335 const char *value
= "0";
1337 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1339 value
, strlen(value
),
1340 (time_t)0, (uint32_t)0);
1341 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1343 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1345 test_true(rc
== MEMCACHED_SUCCESS
);
1346 test_true(new_number
== 1);
1348 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1350 test_true(rc
== MEMCACHED_SUCCESS
);
1351 test_true(new_number
== 2);
1353 return TEST_SUCCESS
;
1356 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1358 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1360 uint64_t new_number
;
1361 memcached_return_t rc
;
1362 const char *master_key
= "foo";
1363 const char *key
= "number";
1364 uint64_t initial
= 0;
1366 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1368 1, initial
, 0, &new_number
);
1369 test_true(rc
== MEMCACHED_SUCCESS
);
1370 test_true(new_number
== initial
);
1372 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1374 1, initial
, 0, &new_number
);
1375 test_true(rc
== MEMCACHED_SUCCESS
);
1376 test_true(new_number
== (initial
+ 1));
1378 return TEST_SUCCESS
;
1381 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1383 uint64_t new_number
;
1384 memcached_return_t rc
;
1385 const char *master_key
= "foo";
1386 const char *key
= "number";
1387 const char *value
= "3";
1389 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1391 value
, strlen(value
),
1392 (time_t)0, (uint32_t)0);
1393 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1395 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1398 test_true(rc
== MEMCACHED_SUCCESS
);
1399 test_true(new_number
== 2);
1401 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1404 test_true(rc
== MEMCACHED_SUCCESS
);
1405 test_true(new_number
== 1);
1407 return TEST_SUCCESS
;
1410 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1412 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1414 uint64_t new_number
;
1415 memcached_return_t rc
;
1416 const char *master_key
= "foo";
1417 const char *key
= "number";
1418 uint64_t initial
= 3;
1420 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1422 1, initial
, 0, &new_number
);
1423 test_true(rc
== MEMCACHED_SUCCESS
);
1424 test_true(new_number
== initial
);
1426 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1428 1, initial
, 0, &new_number
);
1429 test_true(rc
== MEMCACHED_SUCCESS
);
1430 test_true(new_number
== (initial
- 1));
1432 return TEST_SUCCESS
;
1435 static test_return_t
quit_test(memcached_st
*memc
)
1437 memcached_return_t rc
;
1438 const char *key
= "fudge";
1439 const char *value
= "sanford and sun";
1441 rc
= memcached_set(memc
, key
, strlen(key
),
1442 value
, strlen(value
),
1443 (time_t)10, (uint32_t)3);
1444 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1445 memcached_quit(memc
);
1447 rc
= memcached_set(memc
, key
, strlen(key
),
1448 value
, strlen(value
),
1449 (time_t)50, (uint32_t)9);
1450 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1452 return TEST_SUCCESS
;
1455 static test_return_t
mget_result_test(memcached_st
*memc
)
1457 memcached_return_t rc
;
1458 const char *keys
[]= {"fudge", "son", "food"};
1459 size_t key_length
[]= {5, 3, 4};
1462 memcached_result_st results_obj
;
1463 memcached_result_st
*results
;
1465 results
= memcached_result_create(memc
, &results_obj
);
1467 test_true(&results_obj
== results
);
1469 /* We need to empty the server before continueing test */
1470 rc
= memcached_flush(memc
, 0);
1471 test_true(rc
== MEMCACHED_SUCCESS
);
1473 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1474 test_true(rc
== MEMCACHED_SUCCESS
);
1476 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1481 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1482 test_true(!results
);
1483 test_true(rc
== MEMCACHED_END
);
1485 for (x
= 0; x
< 3; x
++)
1487 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1488 keys
[x
], key_length
[x
],
1489 (time_t)50, (uint32_t)9);
1490 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1493 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1494 test_true(rc
== MEMCACHED_SUCCESS
);
1496 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1499 test_true(&results_obj
== results
);
1500 test_true(rc
== MEMCACHED_SUCCESS
);
1501 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1502 test_true(!memcmp(memcached_result_key_value(results
),
1503 memcached_result_value(results
),
1504 memcached_result_length(results
)));
1507 memcached_result_free(&results_obj
);
1509 return TEST_SUCCESS
;
1512 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1514 memcached_return_t rc
;
1515 const char *keys
[]= {"fudge", "son", "food"};
1516 size_t key_length
[]= {5, 3, 4};
1519 memcached_result_st
*results
;
1521 /* We need to empty the server before continueing test */
1522 rc
= memcached_flush(memc
, 0);
1523 test_true(rc
== MEMCACHED_SUCCESS
);
1525 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1526 test_true(rc
== MEMCACHED_SUCCESS
);
1528 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1532 test_true(!results
);
1533 test_true(rc
== MEMCACHED_END
);
1535 for (x
= 0; x
< 3; x
++)
1537 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1538 keys
[x
], key_length
[x
],
1539 (time_t)50, (uint32_t)9);
1540 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1543 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1544 test_true(rc
== MEMCACHED_SUCCESS
);
1547 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1550 test_true(rc
== MEMCACHED_SUCCESS
);
1551 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1552 test_true(!memcmp(memcached_result_key_value(results
),
1553 memcached_result_value(results
),
1554 memcached_result_length(results
)));
1555 memcached_result_free(results
);
1559 return TEST_SUCCESS
;
1562 /* Count the results */
1563 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1564 memcached_result_st
*result
__attribute__((unused
)),
1567 size_t *counter
= (size_t *)context
;
1569 *counter
= *counter
+ 1;
1571 return MEMCACHED_SUCCESS
;
1574 static test_return_t
mget_result_function(memcached_st
*memc
)
1576 memcached_return_t rc
;
1577 const char *keys
[]= {"fudge", "son", "food"};
1578 size_t key_length
[]= {5, 3, 4};
1581 memcached_execute_fn callbacks
[1];
1583 /* We need to empty the server before continueing test */
1584 rc
= memcached_flush(memc
, 0);
1585 for (x
= 0; x
< 3; x
++)
1587 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1588 keys
[x
], key_length
[x
],
1589 (time_t)50, (uint32_t)9);
1590 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1593 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1594 test_true(rc
== MEMCACHED_SUCCESS
);
1596 callbacks
[0]= &callback_counter
;
1598 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1600 test_true(counter
== 3);
1602 return TEST_SUCCESS
;
1605 static test_return_t
mget_test(memcached_st
*memc
)
1607 memcached_return_t rc
;
1608 const char *keys
[]= {"fudge", "son", "food"};
1609 size_t key_length
[]= {5, 3, 4};
1613 char return_key
[MEMCACHED_MAX_KEY
];
1614 size_t return_key_length
;
1616 size_t return_value_length
;
1618 /* We need to empty the server before continueing test */
1619 rc
= memcached_flush(memc
, 0);
1620 test_true(rc
== MEMCACHED_SUCCESS
);
1622 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1623 test_true(rc
== MEMCACHED_SUCCESS
);
1625 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1626 &return_value_length
, &flags
, &rc
)) != NULL
)
1628 test_true(return_value
);
1630 test_true(!return_value
);
1631 test_true(return_value_length
== 0);
1632 test_true(rc
== MEMCACHED_END
);
1634 for (x
= 0; x
< 3; x
++)
1636 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1637 keys
[x
], key_length
[x
],
1638 (time_t)50, (uint32_t)9);
1639 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1642 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1643 test_true(rc
== MEMCACHED_SUCCESS
);
1646 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1647 &return_value_length
, &flags
, &rc
)))
1649 test_true(return_value
);
1650 test_true(rc
== MEMCACHED_SUCCESS
);
1651 test_true(return_key_length
== return_value_length
);
1652 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1657 return TEST_SUCCESS
;
1660 static test_return_t
mget_execute(memcached_st
*memc
)
1664 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1668 * I only want to hit _one_ server so I know the number of requests I'm
1669 * sending in the pipeline.
1671 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1672 memc
->number_of_hosts
= 1;
1674 size_t max_keys
= 20480;
1677 char **keys
= calloc(max_keys
, sizeof(char*));
1678 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1680 /* First add all of the items.. */
1681 char blob
[1024] = {0};
1682 memcached_return_t rc
;
1684 for (size_t x
= 0; x
< max_keys
; ++x
)
1688 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
1690 test_true(keys
[x
] != NULL
);
1691 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1692 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1695 /* Try to get all of them with a large multiget */
1697 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1698 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1699 max_keys
, callbacks
, &counter
, 1);
1701 if (rc
== MEMCACHED_SUCCESS
)
1704 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1705 test_true(rc
== MEMCACHED_END
);
1707 /* Verify that we got all of the items */
1708 test_true(counter
== max_keys
);
1710 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1712 test_true(counter
== 0);
1716 test_fail("note: this test functions differently when in binary mode");
1719 /* Release all allocated resources */
1720 for (size_t x
= 0; x
< max_keys
; ++x
)
1727 memc
->number_of_hosts
= number_of_hosts
;
1728 return TEST_SUCCESS
;
1731 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1733 static test_return_t
key_setup(memcached_st
*memc
)
1737 if (pre_binary(memc
) != TEST_SUCCESS
)
1738 return TEST_SKIPPED
;
1740 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1742 return TEST_SUCCESS
;
1745 static test_return_t
key_teardown(memcached_st
*memc
)
1748 pairs_free(global_pairs
);
1750 return TEST_SUCCESS
;
1753 static test_return_t
block_add_regression(memcached_st
*memc
)
1755 /* First add all of the items.. */
1756 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1758 memcached_return_t rc
;
1759 char blob
[1024] = {0};
1761 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1762 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1765 return TEST_SUCCESS
;
1768 static test_return_t
binary_add_regression(memcached_st
*memc
)
1770 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1771 test_return_t rc
= block_add_regression(memc
);
1772 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1776 static test_return_t
get_stats_keys(memcached_st
*memc
)
1780 memcached_stat_st memc_stat
;
1781 memcached_return_t rc
;
1783 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1784 test_true(rc
== MEMCACHED_SUCCESS
);
1785 for (ptr
= stat_list
; *ptr
; ptr
++)
1790 return TEST_SUCCESS
;
1793 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1795 const char *version_string
;
1797 version_string
= memcached_lib_version();
1799 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1801 return TEST_SUCCESS
;
1804 static test_return_t
get_stats(memcached_st
*memc
)
1808 memcached_return_t rc
;
1809 memcached_stat_st
*memc_stat
;
1811 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1812 test_true(rc
== MEMCACHED_SUCCESS
);
1814 test_true(rc
== MEMCACHED_SUCCESS
);
1815 test_true(memc_stat
);
1817 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1819 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1820 test_true(rc
== MEMCACHED_SUCCESS
);
1821 for (ptr
= stat_list
; *ptr
; ptr
++);
1826 memcached_stat_free(NULL
, memc_stat
);
1828 return TEST_SUCCESS
;
1831 static test_return_t
add_host_test(memcached_st
*memc
)
1834 memcached_server_st
*servers
;
1835 memcached_return_t rc
;
1836 char servername
[]= "0.example.com";
1838 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1840 test_true(1 == memcached_server_list_count(servers
));
1842 for (x
= 2; x
< 20; x
++)
1844 char buffer
[SMALL_STRING_LEN
];
1846 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1847 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1849 test_true(rc
== MEMCACHED_SUCCESS
);
1850 test_true(x
== memcached_server_list_count(servers
));
1853 rc
= memcached_server_push(memc
, servers
);
1854 test_true(rc
== MEMCACHED_SUCCESS
);
1855 rc
= memcached_server_push(memc
, servers
);
1856 test_true(rc
== MEMCACHED_SUCCESS
);
1858 memcached_server_list_free(servers
);
1860 return TEST_SUCCESS
;
1863 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1865 return MEMCACHED_SUCCESS
;
1868 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1870 return MEMCACHED_SUCCESS
;
1873 static test_return_t
callback_test(memcached_st
*memc
)
1875 /* Test User Data */
1879 memcached_return_t rc
;
1881 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1882 test_true(rc
== MEMCACHED_SUCCESS
);
1883 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1884 test_true(*test_ptr
== x
);
1887 /* Test Clone Callback */
1889 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1890 void *clone_cb_ptr
= *(void **)&clone_cb
;
1891 void *temp_function
= NULL
;
1892 memcached_return_t rc
;
1894 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1896 test_true(rc
== MEMCACHED_SUCCESS
);
1897 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1898 test_true(temp_function
== clone_cb_ptr
);
1901 /* Test Cleanup Callback */
1903 memcached_cleanup_fn cleanup_cb
=
1904 (memcached_cleanup_fn
)cleanup_test_callback
;
1905 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1906 void *temp_function
= NULL
;
1907 memcached_return_t rc
;
1909 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1911 test_true(rc
== MEMCACHED_SUCCESS
);
1912 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1913 test_true(temp_function
== cleanup_cb_ptr
);
1916 return TEST_SUCCESS
;
1919 /* We don't test the behavior itself, we test the switches */
1920 static test_return_t
behavior_test(memcached_st
*memc
)
1925 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1926 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1927 test_true(value
== 1);
1929 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1930 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1931 test_true(value
== 1);
1933 set
= MEMCACHED_HASH_MD5
;
1934 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1935 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1936 test_true(value
== MEMCACHED_HASH_MD5
);
1940 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1941 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1942 test_true(value
== 0);
1944 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1945 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1946 test_true(value
== 0);
1948 set
= MEMCACHED_HASH_DEFAULT
;
1949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1950 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1951 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1953 set
= MEMCACHED_HASH_CRC
;
1954 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1955 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1956 test_true(value
== MEMCACHED_HASH_CRC
);
1958 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1959 test_true(value
> 0);
1961 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1962 test_true(value
> 0);
1964 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1965 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1966 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1968 return TEST_SUCCESS
;
1971 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1973 memcached_return_t rc
;
1977 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1978 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1980 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1982 if (rc
== MEMCACHED_SUCCESS
)
1984 test_true((bool)value
== set
);
1988 test_false((bool)value
== set
);
1991 return TEST_SUCCESS
;
1995 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
1997 memcached_return_t rc
;
2001 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2002 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2004 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2006 if (rc
== MEMCACHED_SUCCESS
)
2008 test_true((bool)value
== set
);
2012 test_false((bool)value
== set
);
2015 return TEST_SUCCESS
;
2019 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2021 memcached_return_t rc
;
2025 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2026 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2028 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2030 if (rc
== MEMCACHED_SUCCESS
)
2032 test_true((bool)value
== set
);
2036 test_false((bool)value
== set
);
2039 return TEST_SUCCESS
;
2042 static test_return_t
fetch_all_results(memcached_st
*memc
)
2044 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2045 char return_key
[MEMCACHED_MAX_KEY
];
2046 size_t return_key_length
;
2048 size_t return_value_length
;
2051 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2052 &return_value_length
, &flags
, &rc
)))
2054 test_true(return_value
);
2055 test_true(rc
== MEMCACHED_SUCCESS
);
2059 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
2062 /* Test case provided by Cal Haldenbrand */
2063 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2065 unsigned int setter
= 1;
2067 unsigned long long total
= 0;
2070 char randomstuff
[6 * 1024];
2071 memcached_return_t rc
;
2073 memset(randomstuff
, 0, 6 * 1024);
2075 /* We just keep looking at the same values over and over */
2078 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2079 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2083 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2087 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2088 memset(randomstuff
, 0, 6 * 1024);
2089 test_true(size
< 6 * 1024); /* Being safe here */
2091 for (j
= 0 ; j
< size
;j
++)
2092 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2095 snprintf(key
, sizeof(key
), "%u", x
);
2096 rc
= memcached_set(memc
, key
, strlen(key
),
2097 randomstuff
, strlen(randomstuff
), 10, 0);
2098 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2099 /* If we fail, lets try again */
2100 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2101 rc
= memcached_set(memc
, key
, strlen(key
),
2102 randomstuff
, strlen(randomstuff
), 10, 0);
2103 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2106 return TEST_SUCCESS
;
2109 /* Test case provided by Cal Haldenbrand */
2110 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2112 unsigned int setter
;
2116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2117 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2119 setter
= 20 * 1024576;
2120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2121 setter
= 20 * 1024576;
2122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2123 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2124 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2126 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2129 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2131 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2132 char buffer
[SMALL_STRING_LEN
];
2137 memset(buffer
, 0, SMALL_STRING_LEN
);
2139 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2140 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2141 &val_len
, &flags
, &rc
);
2142 if (rc
!= MEMCACHED_SUCCESS
)
2144 if (rc
== MEMCACHED_NOTFOUND
)
2158 return TEST_SUCCESS
;
2161 /* Do a large mget() over all the keys we think exist */
2162 #define KEY_COUNT 3000 // * 1024576
2163 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2165 memcached_return_t rc
;
2166 unsigned int setter
;
2169 size_t key_lengths
[KEY_COUNT
];
2172 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2173 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2175 setter
= 20 * 1024576;
2176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2177 setter
= 20 * 1024576;
2178 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2179 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2180 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2183 keys
= calloc(KEY_COUNT
, sizeof(char *));
2185 for (x
= 0; x
< KEY_COUNT
; x
++)
2189 snprintf(buffer
, 30, "%u", x
);
2190 keys
[x
]= strdup(buffer
);
2191 key_lengths
[x
]= strlen(keys
[x
]);
2194 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2195 test_true(rc
== MEMCACHED_SUCCESS
);
2197 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2199 for (x
= 0; x
< KEY_COUNT
; x
++)
2203 return TEST_SUCCESS
;
2206 /* Make sure we behave properly if server list has no values */
2207 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2209 memcached_return_t rc
;
2210 const char *keys
[]= {"fudge", "son", "food"};
2211 size_t key_length
[]= {5, 3, 4};
2214 char return_key
[MEMCACHED_MAX_KEY
];
2215 size_t return_key_length
;
2217 size_t return_value_length
;
2219 /* Here we free everything before running a bunch of mget tests */
2220 memcached_servers_reset(memc
);
2223 /* We need to empty the server before continueing test */
2224 rc
= memcached_flush(memc
, 0);
2225 test_true(rc
== MEMCACHED_NO_SERVERS
);
2227 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2228 test_true(rc
== MEMCACHED_NO_SERVERS
);
2230 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2231 &return_value_length
, &flags
, &rc
)) != NULL
)
2233 test_true(return_value
);
2235 test_true(!return_value
);
2236 test_true(return_value_length
== 0);
2237 test_true(rc
== MEMCACHED_NO_SERVERS
);
2239 for (x
= 0; x
< 3; x
++)
2241 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2242 keys
[x
], key_length
[x
],
2243 (time_t)50, (uint32_t)9);
2244 test_true(rc
== MEMCACHED_NO_SERVERS
);
2247 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2248 test_true(rc
== MEMCACHED_NO_SERVERS
);
2251 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2252 &return_value_length
, &flags
, &rc
)))
2254 test_true(return_value
);
2255 test_true(rc
== MEMCACHED_SUCCESS
);
2256 test_true(return_key_length
== return_value_length
);
2257 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2262 return TEST_SUCCESS
;
2265 #define VALUE_SIZE_BUG5 1048064
2266 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2268 memcached_return_t rc
;
2269 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2270 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2271 char return_key
[MEMCACHED_MAX_KEY
];
2272 size_t return_key_length
;
2274 size_t value_length
;
2278 char insert_data
[VALUE_SIZE_BUG5
];
2280 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2281 insert_data
[x
]= (signed char)rand();
2283 memcached_flush(memc
, 0);
2284 value
= memcached_get(memc
, keys
[0], key_length
[0],
2285 &value_length
, &flags
, &rc
);
2286 test_true(value
== NULL
);
2287 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2290 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2291 &value_length
, &flags
, &rc
)))
2293 test_true(count
== 0);
2295 for (x
= 0; x
< 4; x
++)
2297 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2298 insert_data
, VALUE_SIZE_BUG5
,
2299 (time_t)0, (uint32_t)0);
2300 test_true(rc
== MEMCACHED_SUCCESS
);
2303 for (x
= 0; x
< 10; x
++)
2305 value
= memcached_get(memc
, keys
[0], key_length
[0],
2306 &value_length
, &flags
, &rc
);
2310 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2312 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2313 &value_length
, &flags
, &rc
)))
2318 test_true(count
== 4);
2321 return TEST_SUCCESS
;
2324 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2326 memcached_return_t rc
;
2327 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2328 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2329 char return_key
[MEMCACHED_MAX_KEY
];
2330 size_t return_key_length
;
2332 size_t value_length
;
2336 char insert_data
[VALUE_SIZE_BUG5
];
2338 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2339 insert_data
[x
]= (signed char)rand();
2341 memcached_flush(memc
, 0);
2342 value
= memcached_get(memc
, keys
[0], key_length
[0],
2343 &value_length
, &flags
, &rc
);
2344 test_true(value
== NULL
);
2345 test_true(rc
== MEMCACHED_NOTFOUND
);
2346 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2347 test_true(rc
== MEMCACHED_SUCCESS
);
2350 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2351 &value_length
, &flags
, &rc
)))
2353 test_true(count
== 0);
2354 test_true(rc
== MEMCACHED_END
);
2356 for (x
= 0; x
< 4; x
++)
2358 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2359 insert_data
, VALUE_SIZE_BUG5
,
2360 (time_t)0, (uint32_t)0);
2361 test_true(rc
== MEMCACHED_SUCCESS
);
2364 for (x
= 0; x
< 2; x
++)
2366 value
= memcached_get(memc
, keys
[0], key_length
[0],
2367 &value_length
, &flags
, &rc
);
2371 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2372 test_true(rc
== MEMCACHED_SUCCESS
);
2374 /* We test for purge of partial complete fetches */
2375 for (count
= 3; count
; count
--)
2377 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2378 &value_length
, &flags
, &rc
);
2379 test_true(rc
== MEMCACHED_SUCCESS
);
2380 test_true(!(memcmp(value
, insert_data
, value_length
)));
2381 test_true(value_length
);
2386 return TEST_SUCCESS
;
2389 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2391 memcached_return_t rc
;
2393 memcached_st
*memc_clone
;
2395 memcached_server_st
*servers
;
2396 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";
2398 servers
= memcached_servers_parse(server_list
);
2401 mine
= memcached_create(NULL
);
2402 rc
= memcached_server_push(mine
, servers
);
2403 test_true(rc
== MEMCACHED_SUCCESS
);
2404 memcached_server_list_free(servers
);
2407 memc_clone
= memcached_clone(NULL
, mine
);
2409 memcached_quit(mine
);
2410 memcached_quit(memc_clone
);
2413 memcached_free(mine
);
2414 memcached_free(memc_clone
);
2416 return TEST_SUCCESS
;
2419 /* Test flag store/retrieve */
2420 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2422 memcached_return_t rc
;
2423 const char *keys
= "036790384900";
2424 size_t key_length
= strlen(keys
);
2425 char return_key
[MEMCACHED_MAX_KEY
];
2426 size_t return_key_length
;
2428 size_t value_length
;
2431 char insert_data
[VALUE_SIZE_BUG5
];
2433 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2434 insert_data
[x
]= (signed char)rand();
2436 memcached_flush(memc
, 0);
2439 rc
= memcached_set(memc
, keys
, key_length
,
2440 insert_data
, VALUE_SIZE_BUG5
,
2442 test_true(rc
== MEMCACHED_SUCCESS
);
2445 value
= memcached_get(memc
, keys
, key_length
,
2446 &value_length
, &flags
, &rc
);
2447 test_true(flags
== 245);
2451 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2454 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2455 &value_length
, &flags
, &rc
);
2456 test_true(flags
== 245);
2461 return TEST_SUCCESS
;
2464 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2466 memcached_return_t rc
;
2467 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2468 size_t key_length
[3];
2473 char return_key
[MEMCACHED_MAX_KEY
];
2474 size_t return_key_length
;
2476 size_t return_value_length
;
2479 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2480 key_length
[1]= strlen("fudge&*@#");
2481 key_length
[2]= strlen("for^#@&$not");
2484 for (x
= 0; x
< 3; x
++)
2486 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2487 keys
[x
], key_length
[x
],
2488 (time_t)50, (uint32_t)9);
2489 test_true(rc
== MEMCACHED_SUCCESS
);
2492 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2493 test_true(rc
== MEMCACHED_SUCCESS
);
2495 /* We need to empty the server before continueing test */
2496 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2497 &return_value_length
, &flags
, &rc
)) != NULL
)
2499 test_true(return_value
);
2503 test_true(count
== 3);
2505 return TEST_SUCCESS
;
2508 /* We are testing with aggressive timeout to get failures */
2509 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2511 const char *key
= "foo";
2513 size_t value_length
= 512;
2516 memcached_return_t rc
;
2517 unsigned int set
= 1;
2518 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2521 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2522 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2524 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2527 value
= (char*)malloc(value_length
* sizeof(char));
2529 for (x
= 0; x
< value_length
; x
++)
2530 value
[x
]= (char) (x
% 127);
2532 for (x
= 1; x
<= 100000; ++x
)
2534 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2536 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2537 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2539 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2544 memcached_free(mclone
);
2546 return TEST_SUCCESS
;
2550 We are looking failures in the async protocol
2552 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2554 const char *key
= "foo";
2556 size_t value_length
= 512;
2559 memcached_return_t rc
;
2560 unsigned int set
= 1;
2562 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2564 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2565 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2567 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2570 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2572 test_true(timeout
== -1);
2574 value
= (char*)malloc(value_length
* sizeof(char));
2576 for (x
= 0; x
< value_length
; x
++)
2577 value
[x
]= (char) (x
% 127);
2579 for (x
= 1; x
<= 100000; ++x
)
2581 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2585 memcached_free(mclone
);
2587 return TEST_SUCCESS
;
2591 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2593 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2595 memcached_return_t rc
;
2597 size_t value_length
;
2599 uint64_t number_value
;
2601 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2602 &value_length
, &flags
, &rc
);
2603 test_true(value
== NULL
);
2604 test_true(rc
== MEMCACHED_NOTFOUND
);
2606 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2609 test_true(value
== NULL
);
2610 /* The binary protocol will set the key if it doesn't exist */
2611 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2613 test_true(rc
== MEMCACHED_SUCCESS
);
2617 test_true(rc
== MEMCACHED_NOTFOUND
);
2620 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2622 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2623 &value_length
, &flags
, &rc
);
2625 test_true(rc
== MEMCACHED_SUCCESS
);
2628 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2630 test_true(number_value
== 2);
2631 test_true(rc
== MEMCACHED_SUCCESS
);
2633 return TEST_SUCCESS
;
2637 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2638 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2640 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2642 char key
[] = "key34567890";
2644 memcached_return_t rc
;
2645 size_t overflowSize
;
2647 char commandFirst
[]= "set key34567890 0 0 ";
2648 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2649 size_t commandLength
;
2652 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2654 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2656 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2658 overflow
= malloc(testSize
);
2659 test_true(overflow
!= NULL
);
2661 memset(overflow
, 'x', testSize
);
2662 rc
= memcached_set(memc
, key
, strlen(key
),
2663 overflow
, testSize
, 0, 0);
2664 test_true(rc
== MEMCACHED_SUCCESS
);
2668 return TEST_SUCCESS
;
2673 Test values of many different sizes
2674 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2675 set key34567890 0 0 8169 \r\n
2676 is sent followed by buffer of size 8169, followed by 8169
2678 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2681 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2682 memcached_return_t rc
;
2683 const char *key
= "foo";
2685 size_t value_length
= 18000;
2687 size_t string_length
;
2690 size_t current_length
;
2692 value
= (char*)malloc(value_length
);
2695 for (x
= 0; x
< value_length
; x
++)
2696 value
[x
] = (char) (x
% 127);
2698 for (current_length
= 0; current_length
< value_length
; current_length
++)
2700 rc
= memcached_set(memc
, key
, strlen(key
),
2701 value
, current_length
,
2702 (time_t)0, (uint32_t)0);
2703 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2705 string
= memcached_get(memc
, key
, strlen(key
),
2706 &string_length
, &flags
, &rc
);
2708 test_true(rc
== MEMCACHED_SUCCESS
);
2709 test_true(string_length
== current_length
);
2710 test_true(!memcmp(string
, value
, string_length
));
2717 return TEST_SUCCESS
;
2721 Look for zero length value problems
2723 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2726 memcached_return_t rc
;
2727 const char *key
= "mykey";
2732 for (x
= 0; x
< 2; x
++)
2734 rc
= memcached_set(memc
, key
, strlen(key
),
2736 (time_t)0, (uint32_t)0);
2738 test_true(rc
== MEMCACHED_SUCCESS
);
2740 value
= memcached_get(memc
, key
, strlen(key
),
2741 &length
, &flags
, &rc
);
2743 test_true(rc
== MEMCACHED_SUCCESS
);
2744 test_true(value
== NULL
);
2745 test_true(length
== 0);
2746 test_true(flags
== 0);
2748 value
= memcached_get(memc
, key
, strlen(key
),
2749 &length
, &flags
, &rc
);
2751 test_true(rc
== MEMCACHED_SUCCESS
);
2752 test_true(value
== NULL
);
2753 test_true(length
== 0);
2754 test_true(flags
== 0);
2757 return TEST_SUCCESS
;
2760 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2761 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2763 memcached_return_t rc
;
2764 const char *key
= "mykey";
2769 rc
= memcached_set(memc
, key
, strlen(key
),
2771 (time_t)0, UINT32_MAX
);
2773 test_true(rc
== MEMCACHED_SUCCESS
);
2775 value
= memcached_get(memc
, key
, strlen(key
),
2776 &length
, &flags
, &rc
);
2778 test_true(rc
== MEMCACHED_SUCCESS
);
2779 test_true(value
== NULL
);
2780 test_true(length
== 0);
2781 test_true(flags
== UINT32_MAX
);
2783 return TEST_SUCCESS
;
2787 /* Check the validity of chinese key*/
2788 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2790 memcached_return_t rc
;
2791 const char *key
= "豆瓣";
2792 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2797 rc
= memcached_set(memc
, key
, strlen(key
),
2798 value
, strlen(value
),
2801 test_true(rc
== MEMCACHED_SUCCESS
);
2803 value2
= memcached_get(memc
, key
, strlen(key
),
2804 &length
, &flags
, &rc
);
2806 test_true(length
==strlen(value
));
2807 test_true(rc
== MEMCACHED_SUCCESS
);
2808 test_true(memcmp(value
, value2
, length
)==0);
2811 return TEST_SUCCESS
;
2819 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2822 const memcached_server_st
*server
;
2823 memcached_return_t res
;
2827 memc
= memcached_create(NULL
);
2828 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2829 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2831 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2833 memcached_free(memc
);
2835 return TEST_SUCCESS
;
2838 /* CAS test from Andei */
2839 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2841 memcached_return_t status
;
2842 memcached_result_st
*result
, result_obj
;
2843 const char *key
= "abc";
2844 size_t key_len
= strlen("abc");
2845 const char *value
= "foobar";
2846 size_t value_len
= strlen(value
);
2848 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2850 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2851 test_true(status
== MEMCACHED_SUCCESS
);
2853 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2854 test_true(status
== MEMCACHED_SUCCESS
);
2856 result
= memcached_result_create(memc
, &result_obj
);
2859 memcached_result_create(memc
, &result_obj
);
2860 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2863 test_true(status
== MEMCACHED_SUCCESS
);
2865 memcached_result_free(result
);
2867 return TEST_SUCCESS
;
2870 #include "ketama_test_cases.h"
2871 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2873 memcached_return_t rc
;
2876 memcached_server_st
*server_pool
;
2881 memc
= memcached_create(NULL
);
2884 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2885 test_true(rc
== MEMCACHED_SUCCESS
);
2887 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2888 test_true(value
== 1);
2890 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2891 test_true(rc
== MEMCACHED_SUCCESS
);
2893 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2894 test_true(value
== MEMCACHED_HASH_MD5
);
2896 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");
2897 memcached_server_push(memc
, server_pool
);
2899 /* verify that the server list was parsed okay. */
2900 test_true(memcached_server_count(memc
) == 8);
2901 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2902 test_true(server_pool
[0].port
== 11211);
2903 test_true(server_pool
[0].weight
== 600);
2904 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2905 test_true(server_pool
[2].port
== 11211);
2906 test_true(server_pool
[2].weight
== 200);
2907 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2908 test_true(server_pool
[7].port
== 11211);
2909 test_true(server_pool
[7].weight
== 100);
2911 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2912 * us test the boundary wraparound.
2914 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2916 /* verify the standard ketama set. */
2917 for (x
= 0; x
< 99; x
++)
2919 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2921 memcached_server_instance_st instance
=
2922 memcached_server_instance_by_position(memc
, server_idx
);
2924 const char *hostname
= memcached_server_name(instance
);
2925 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2928 memcached_server_list_free(server_pool
);
2929 memcached_free(memc
);
2931 return TEST_SUCCESS
;
2934 /* Large mget() of missing keys with binary proto
2936 * If many binary quiet commands (such as getq's in an mget) fill the output
2937 * buffer and the server chooses not to respond, memcached_flush hangs. See
2938 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2941 /* sighandler_t function that always asserts false */
2942 static void fail(int unused
__attribute__((unused
)))
2948 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2950 memcached_return_t rc
;
2953 size_t* key_lengths
;
2954 void (*oldalarm
)(int);
2955 memcached_st
*memc_clone
;
2957 memc_clone
= memcached_clone(NULL
, memc
);
2958 test_true(memc_clone
);
2960 /* only binproto uses getq for mget */
2961 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2963 /* empty the cache to ensure misses (hence non-responses) */
2964 rc
= memcached_flush(memc_clone
, 0);
2965 test_true(rc
== MEMCACHED_SUCCESS
);
2967 key_lengths
= calloc(key_count
, sizeof(size_t));
2968 keys
= calloc(key_count
, sizeof(char *));
2970 for (x
= 0; x
< key_count
; x
++)
2974 snprintf(buffer
, 30, "%u", x
);
2975 keys
[x
]= strdup(buffer
);
2976 key_lengths
[x
]= strlen(keys
[x
]);
2979 oldalarm
= signal(SIGALRM
, fail
);
2982 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2983 test_true(rc
== MEMCACHED_SUCCESS
);
2986 signal(SIGALRM
, oldalarm
);
2988 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2990 for (x
= 0; x
< key_count
; x
++)
2995 memcached_free(memc_clone
);
2997 return TEST_SUCCESS
;
3000 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3002 test_return_t test_rc
;
3003 test_rc
= pre_binary(memc
);
3005 if (test_rc
!= TEST_SUCCESS
)
3010 /* should work as of r580 */
3011 rc
= _user_supplied_bug21(memc
, 10);
3012 test_true(rc
== TEST_SUCCESS
);
3014 /* should fail as of r580 */
3015 rc
= _user_supplied_bug21(memc
, 1000);
3016 test_true(rc
== TEST_SUCCESS
);
3018 return TEST_SUCCESS
;
3021 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3024 memcached_server_instance_st instance
;
3026 memcached_return_t rc
;
3027 memcached_st
*memc
= memcached_create(NULL
);
3030 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3031 test_true(rc
== MEMCACHED_SUCCESS
);
3033 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3034 test_true(value
== 1);
3036 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3037 test_true(rc
== MEMCACHED_SUCCESS
);
3039 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3040 test_true(value
== MEMCACHED_HASH_MD5
);
3042 /* server should be removed when in delay */
3043 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3044 test_true(rc
== MEMCACHED_SUCCESS
);
3046 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3047 test_true(value
== 1);
3049 memcached_server_st
*server_pool
;
3050 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");
3051 memcached_server_push(memc
, server_pool
);
3053 /* verify that the server list was parsed okay. */
3054 test_true(memcached_server_count(memc
) == 8);
3055 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3056 test_true(server_pool
[0].port
== 11211);
3057 test_true(server_pool
[0].weight
== 600);
3058 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3059 test_true(server_pool
[2].port
== 11211);
3060 test_true(server_pool
[2].weight
== 200);
3061 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3062 test_true(server_pool
[7].port
== 11211);
3063 test_true(server_pool
[7].weight
== 100);
3065 instance
= memcached_server_instance_by_position(memc
, 2);
3066 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3067 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3070 This would not work if there were only two hosts.
3072 for (size_t x
= 0; x
< 99; x
++)
3074 memcached_autoeject(memc
);
3075 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3076 test_true(server_idx
!= 2);
3079 /* and re-added when it's back. */
3080 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3081 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3082 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3083 memc
->distribution
);
3084 for (size_t x
= 0; x
< 99; x
++)
3086 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3087 // We re-use instance from above.
3089 memcached_server_instance_by_position(memc
, server_idx
);
3090 const char *hostname
= memcached_server_name(instance
);
3091 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3094 memcached_server_list_free(server_pool
);
3095 memcached_free(memc
);
3097 return TEST_SUCCESS
;
3100 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3104 memcached_return_t rc
;
3105 memcached_st
*memc
= memcached_create(NULL
);
3109 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3110 test_true(rc
== MEMCACHED_SUCCESS
);
3112 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3113 test_true(value
== 1);
3115 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3116 test_true(rc
== MEMCACHED_SUCCESS
);
3118 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3119 test_true(value
== MEMCACHED_HASH_MD5
);
3122 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3124 memcached_server_st
*server_pool
;
3125 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");
3126 memcached_server_push(memc
, server_pool
);
3128 // @todo this needs to be refactored to actually test something.
3131 if ((fp
= fopen("ketama_keys.txt", "w")))
3135 printf("cannot write to file ketama_keys.txt");
3136 return TEST_FAILURE
;
3139 for (int x
= 0; x
< 10000; x
++)
3142 sprintf(key
, "%d", x
);
3144 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3145 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3146 in_port_t port
= memc
->hosts
[server_idx
].port
;
3147 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3148 memcached_server_instance_st instance
=
3149 memcached_server_instance_by_position(memc
, host_index
);
3153 memcached_server_list_free(server_pool
);
3154 memcached_free(memc
);
3156 return TEST_SUCCESS
;
3160 static test_return_t
result_static(memcached_st
*memc
)
3162 memcached_result_st result
;
3163 memcached_result_st
*result_ptr
;
3165 result_ptr
= memcached_result_create(memc
, &result
);
3166 test_true(result
.options
.is_allocated
== false);
3167 test_true(memcached_is_initialized(&result
) == true);
3168 test_true(result_ptr
);
3169 test_true(result_ptr
== &result
);
3171 memcached_result_free(&result
);
3173 test_true(result
.options
.is_allocated
== false);
3174 test_true(memcached_is_initialized(&result
) == false);
3176 return TEST_SUCCESS
;
3179 static test_return_t
result_alloc(memcached_st
*memc
)
3181 memcached_result_st
*result_ptr
;
3183 result_ptr
= memcached_result_create(memc
, NULL
);
3184 test_true(result_ptr
);
3185 test_true(result_ptr
->options
.is_allocated
== true);
3186 test_true(memcached_is_initialized(result_ptr
) == true);
3187 memcached_result_free(result_ptr
);
3189 return TEST_SUCCESS
;
3192 static test_return_t
string_static_null(memcached_st
*memc
)
3194 memcached_string_st string
;
3195 memcached_string_st
*string_ptr
;
3197 string_ptr
= memcached_string_create(memc
, &string
, 0);
3198 test_true(string
.options
.is_initialized
== true);
3199 test_true(string_ptr
);
3201 /* The following two better be the same! */
3202 test_true(memcached_is_allocated(string_ptr
) == false);
3203 test_true(memcached_is_allocated(&string
) == false);
3204 test_true(&string
== string_ptr
);
3206 test_true(string
.options
.is_initialized
== true);
3207 test_true(memcached_is_initialized(&string
) == true);
3208 memcached_string_free(&string
);
3209 test_true(memcached_is_initialized(&string
) == false);
3211 return TEST_SUCCESS
;
3214 static test_return_t
string_alloc_null(memcached_st
*memc
)
3216 memcached_string_st
*string
;
3218 string
= memcached_string_create(memc
, NULL
, 0);
3220 test_true(memcached_is_allocated(string
) == true);
3221 test_true(memcached_is_initialized(string
) == true);
3222 memcached_string_free(string
);
3224 return TEST_SUCCESS
;
3227 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3229 memcached_string_st
*string
;
3231 string
= memcached_string_create(memc
, NULL
, 1024);
3233 test_true(memcached_is_allocated(string
) == true);
3234 test_true(memcached_is_initialized(string
) == true);
3235 memcached_string_free(string
);
3237 return TEST_SUCCESS
;
3240 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3242 memcached_string_st
*string
;
3244 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3245 test_true(string
== NULL
);
3247 return TEST_SUCCESS
;
3250 static test_return_t
string_alloc_append(memcached_st
*memc
)
3253 char buffer
[SMALL_STRING_LEN
];
3254 memcached_string_st
*string
;
3256 /* Ring the bell! */
3257 memset(buffer
, 6, SMALL_STRING_LEN
);
3259 string
= memcached_string_create(memc
, NULL
, 100);
3261 test_true(memcached_is_allocated(string
) == true);
3262 test_true(memcached_is_initialized(string
) == true);
3264 for (x
= 0; x
< 1024; x
++)
3266 memcached_return_t rc
;
3267 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3268 test_true(rc
== MEMCACHED_SUCCESS
);
3270 test_true(memcached_is_allocated(string
) == true);
3271 memcached_string_free(string
);
3273 return TEST_SUCCESS
;
3276 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3278 memcached_return_t rc
;
3280 char buffer
[SMALL_STRING_LEN
];
3281 memcached_string_st
*string
;
3283 /* Ring the bell! */
3284 memset(buffer
, 6, SMALL_STRING_LEN
);
3286 string
= memcached_string_create(memc
, NULL
, 100);
3288 test_true(memcached_is_allocated(string
) == true);
3289 test_true(memcached_is_initialized(string
) == true);
3291 for (x
= 0; x
< 1024; x
++)
3293 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3294 test_true(rc
== MEMCACHED_SUCCESS
);
3296 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3297 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3298 test_true(memcached_is_allocated(string
) == true);
3299 memcached_string_free(string
);
3301 return TEST_SUCCESS
;
3304 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3306 pairs_free(global_pairs
);
3308 return TEST_SUCCESS
;
3311 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3313 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3314 global_count
= GLOBAL_COUNT
;
3316 for (size_t x
= 0; x
< global_count
; x
++)
3318 global_keys
[x
]= global_pairs
[x
].key
;
3319 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3322 return TEST_SUCCESS
;
3325 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3327 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3328 global_count
= GLOBAL2_COUNT
;
3330 for (size_t x
= 0; x
< global_count
; x
++)
3332 global_keys
[x
]= global_pairs
[x
].key
;
3333 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3336 return TEST_SUCCESS
;
3339 static test_return_t
generate_data(memcached_st
*memc
)
3341 execute_set(memc
, global_pairs
, global_count
);
3343 return TEST_SUCCESS
;
3346 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3348 memcached_stat_st
*stat_p
;
3349 memcached_return_t rc
;
3350 uint32_t host_index
= 0;
3351 execute_set(memc
, global_pairs
, global_count
);
3353 //TODO: hosts used size stats
3354 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3357 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3359 /* This test was changes so that "make test" would work properlly */
3361 memcached_server_instance_st instance
=
3362 memcached_server_instance_by_position(memc
, host_index
);
3364 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3366 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3369 memcached_stat_free(NULL
, stat_p
);
3371 return TEST_SUCCESS
;
3373 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3378 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3379 generate_data(memc
);
3381 return TEST_SUCCESS
;
3384 static test_return_t
get_read_count(memcached_st
*memc
)
3386 memcached_return_t rc
;
3387 memcached_st
*memc_clone
;
3389 memc_clone
= memcached_clone(NULL
, memc
);
3390 test_true(memc_clone
);
3392 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3396 size_t return_value_length
;
3400 for (size_t x
= count
= 0; x
< global_count
; x
++)
3402 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3403 &return_value_length
, &flags
, &rc
);
3404 if (rc
== MEMCACHED_SUCCESS
)
3413 memcached_free(memc_clone
);
3415 return TEST_SUCCESS
;
3418 static test_return_t
get_read(memcached_st
*memc
)
3420 memcached_return_t rc
;
3424 size_t return_value_length
;
3427 for (size_t x
= 0; x
< global_count
; x
++)
3429 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3430 &return_value_length
, &flags
, &rc
);
3432 test_true(return_value);
3433 test_true(rc == MEMCACHED_SUCCESS);
3435 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3440 return TEST_SUCCESS
;
3443 static test_return_t
mget_read(memcached_st
*memc
)
3445 memcached_return_t rc
;
3447 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3448 test_true(rc
== MEMCACHED_SUCCESS
);
3449 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3451 return TEST_SUCCESS
;
3454 static test_return_t
mget_read_result(memcached_st
*memc
)
3456 memcached_return_t rc
;
3458 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3459 test_true(rc
== MEMCACHED_SUCCESS
);
3460 /* Turn this into a help function */
3462 memcached_result_st results_obj
;
3463 memcached_result_st
*results
;
3465 results
= memcached_result_create(memc
, &results_obj
);
3467 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3470 test_true(rc
== MEMCACHED_SUCCESS
);
3473 memcached_result_free(&results_obj
);
3476 return TEST_SUCCESS
;
3479 static test_return_t
mget_read_function(memcached_st
*memc
)
3481 memcached_return_t rc
;
3483 memcached_execute_fn callbacks
[1];
3485 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3486 test_true(rc
== MEMCACHED_SUCCESS
);
3488 callbacks
[0]= &callback_counter
;
3490 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3492 return TEST_SUCCESS
;
3495 static test_return_t
delete_generate(memcached_st
*memc
)
3497 for (size_t x
= 0; x
< global_count
; x
++)
3499 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3502 return TEST_SUCCESS
;
3505 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3510 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3512 for (size_t x
= 0; x
< global_count
; x
++)
3514 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3517 return TEST_SUCCESS
;
3520 static test_return_t
add_host_test1(memcached_st
*memc
)
3522 memcached_return_t rc
;
3523 char servername
[]= "0.example.com";
3524 memcached_server_st
*servers
;
3526 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3528 test_true(1 == memcached_server_list_count(servers
));
3530 for (size_t x
= 2; x
< 20; x
++)
3532 char buffer
[SMALL_STRING_LEN
];
3534 snprintf(buffer
, SMALL_STRING_LEN
, "%zu.example.com", 400+x
);
3535 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3537 test_true(rc
== MEMCACHED_SUCCESS
);
3538 test_true(x
== memcached_server_list_count(servers
));
3541 rc
= memcached_server_push(memc
, servers
);
3542 test_true(rc
== MEMCACHED_SUCCESS
);
3543 rc
= memcached_server_push(memc
, servers
);
3544 test_true(rc
== MEMCACHED_SUCCESS
);
3546 memcached_server_list_free(servers
);
3548 return TEST_SUCCESS
;
3551 static test_return_t
pre_nonblock(memcached_st
*memc
)
3553 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3555 return TEST_SUCCESS
;
3558 static test_return_t
pre_cork(memcached_st
*memc
)
3560 memcached_return_t rc
;
3563 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3566 return TEST_SKIPPED
;
3569 if (rc
== MEMCACHED_SUCCESS
)
3570 return TEST_SUCCESS
;
3572 return TEST_SKIPPED
;
3575 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3582 return TEST_SKIPPED
;
3585 if (rc
!= TEST_SUCCESS
)
3588 return pre_nonblock(memc
);
3591 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3593 memcached_return_t rc
= MEMCACHED_FAILURE
;
3594 memcached_st
*memc_clone
;
3595 memcached_server_instance_st instance
;
3597 memc_clone
= memcached_clone(NULL
, memc
);
3598 test_true(memc_clone
);
3599 // The memcached_version needs to be done on a clone, because the server
3600 // will not toggle protocol on an connection.
3601 memcached_version(memc_clone
);
3603 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3605 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3607 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3608 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3609 test_true(rc
== MEMCACHED_SUCCESS
);
3610 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3614 return TEST_SKIPPED
;
3617 memcached_free(memc_clone
);
3619 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3622 static test_return_t
pre_murmur(memcached_st
*memc
)
3624 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3626 return TEST_SUCCESS
;
3629 static test_return_t
pre_jenkins(memcached_st
*memc
)
3631 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3633 return TEST_SUCCESS
;
3637 static test_return_t
pre_md5(memcached_st
*memc
)
3639 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3641 return TEST_SUCCESS
;
3644 static test_return_t
pre_crc(memcached_st
*memc
)
3646 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3648 return TEST_SUCCESS
;
3651 static test_return_t
pre_hsieh(memcached_st
*memc
)
3653 #ifdef HAVE_HSIEH_HASH
3654 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3655 return TEST_SUCCESS
;
3658 return TEST_SKIPPED
;
3662 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3664 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3666 return TEST_SUCCESS
;
3669 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3671 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3673 return TEST_SUCCESS
;
3676 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3678 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3680 return TEST_SUCCESS
;
3683 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3685 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3687 return TEST_SUCCESS
;
3690 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3692 memcached_return_t rc
;
3695 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3696 test_true(rc
== MEMCACHED_SUCCESS
);
3698 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3699 test_true(value
== 1);
3701 return TEST_SUCCESS
;
3704 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3706 memcached_return_t rc
;
3709 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3710 test_true(rc
== MEMCACHED_SUCCESS
);
3712 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3713 test_true(value
== 1);
3715 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3716 test_true(rc
== MEMCACHED_SUCCESS
);
3718 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3719 test_true(value
== MEMCACHED_HASH_MD5
);
3721 return TEST_SUCCESS
;
3725 @note This should be testing to see if the server really supports the binary protocol.
3727 static test_return_t
pre_binary(memcached_st
*memc
)
3729 memcached_return_t rc
= MEMCACHED_FAILURE
;
3730 memcached_st
*memc_clone
;
3731 memcached_server_instance_st instance
;
3733 memc_clone
= memcached_clone(NULL
, memc
);
3734 test_true(memc_clone
);
3735 // The memcached_version needs to be done on a clone, because the server
3736 // will not toggle protocol on an connection.
3737 memcached_version(memc_clone
);
3739 instance
= memcached_server_instance_by_position(memc_clone
, 0);
3741 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
3743 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3744 test_true(rc
== MEMCACHED_SUCCESS
);
3745 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3748 memcached_free(memc_clone
);
3750 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3753 static test_return_t
pre_sasl(memcached_st
*memc
)
3755 memcached_return_t rc
= MEMCACHED_FAILURE
;
3757 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3758 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3759 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3760 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3762 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3764 memcached_server_st
*servers
= memcached_servers_parse(server
);
3765 test_true(servers
!= NULL
);
3766 memcached_servers_reset(memc
);
3767 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3768 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3769 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3770 test_true(rc
== MEMCACHED_SUCCESS
);
3776 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3779 static test_return_t
pre_replication(memcached_st
*memc
)
3781 test_return_t test_rc
;
3782 test_rc
= pre_binary(memc
);
3784 if (test_rc
!= TEST_SUCCESS
)
3788 * Make sure that we store the item on all servers
3789 * (master + replicas == number of servers)
3791 memcached_return_t rc
;
3792 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3793 memcached_server_count(memc
) - 1);
3794 test_true(rc
== MEMCACHED_SUCCESS
);
3795 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3797 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3801 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3805 rc
= pre_replication(memc
);
3806 if (rc
!= TEST_SUCCESS
)
3809 rc
= pre_nonblock(memc
);
3815 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3818 #ifdef HARD_MALLOC_TESTS
3819 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3827 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3830 #ifdef HARD_MALLOC_TESTS
3831 void *ret
= malloc(size
+ 8);
3834 ret
= (void*)((caddr_t
)ret
+ 8);
3837 void *ret
= malloc(size
);
3842 memset(ret
, 0xff, size
);
3849 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3852 #ifdef HARD_MALLOC_TESTS
3853 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3854 void *nmem
= realloc(real_ptr
, size
+ 8);
3859 ret
= (void*)((caddr_t
)nmem
+ 8);
3864 return realloc(mem
, size
);
3869 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3872 #ifdef HARD_MALLOC_TESTS
3873 void *mem
= my_malloc(ptr
, nelem
* size
);
3876 memset(mem
, 0, nelem
* size
);
3881 return calloc(nelem
, size
);
3886 static test_return_t
set_prefix(memcached_st
*memc
)
3888 memcached_return_t rc
;
3889 const char *key
= "mine";
3892 /* Make sure be default none exists */
3893 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3894 test_true(rc
== MEMCACHED_FAILURE
);
3896 /* Test a clean set */
3897 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3898 test_true(rc
== MEMCACHED_SUCCESS
);
3900 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3901 test_true(memcmp(value
, key
, 4) == 0);
3902 test_true(rc
== MEMCACHED_SUCCESS
);
3904 /* Test that we can turn it off */
3905 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3906 test_true(rc
== MEMCACHED_SUCCESS
);
3908 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3909 test_true(rc
== MEMCACHED_FAILURE
);
3911 /* Now setup for main test */
3912 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3913 test_true(rc
== MEMCACHED_SUCCESS
);
3915 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3916 test_true(rc
== MEMCACHED_SUCCESS
);
3917 test_true(memcmp(value
, key
, 4) == 0);
3919 /* Set to Zero, and then Set to something too large */
3922 memset(long_key
, 0, 255);
3924 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3925 test_true(rc
== MEMCACHED_SUCCESS
);
3927 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3928 test_true(rc
== MEMCACHED_FAILURE
);
3929 test_true(value
== NULL
);
3931 /* Test a long key for failure */
3932 /* TODO, extend test to determine based on setting, what result should be */
3933 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3934 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3935 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3936 test_true(rc
== MEMCACHED_SUCCESS
);
3938 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3939 strcpy(long_key
, "This is more then the allotted number of characters");
3940 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3941 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3943 /* Test for a bad prefix, but with a short key */
3944 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3945 test_true(rc
== MEMCACHED_SUCCESS
);
3947 strcpy(long_key
, "dog cat");
3948 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3949 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3952 return TEST_SUCCESS
;
3956 #ifdef MEMCACHED_ENABLE_DEPRECATED
3957 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3959 void *test_ptr
= NULL
;
3962 memcached_malloc_fn malloc_cb
=
3963 (memcached_malloc_fn
)my_malloc
;
3964 cb_ptr
= *(void **)&malloc_cb
;
3965 memcached_return_t rc
;
3967 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3968 test_true(rc
== MEMCACHED_SUCCESS
);
3969 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3970 test_true(rc
== MEMCACHED_SUCCESS
);
3971 test_true(test_ptr
== cb_ptr
);
3975 memcached_realloc_fn realloc_cb
=
3976 (memcached_realloc_fn
)my_realloc
;
3977 cb_ptr
= *(void **)&realloc_cb
;
3978 memcached_return_t rc
;
3980 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3981 test_true(rc
== MEMCACHED_SUCCESS
);
3982 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3983 test_true(rc
== MEMCACHED_SUCCESS
);
3984 test_true(test_ptr
== cb_ptr
);
3988 memcached_free_fn free_cb
=
3989 (memcached_free_fn
)my_free
;
3990 cb_ptr
= *(void **)&free_cb
;
3991 memcached_return_t rc
;
3993 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3994 test_true(rc
== MEMCACHED_SUCCESS
);
3995 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3996 test_true(rc
== MEMCACHED_SUCCESS
);
3997 test_true(test_ptr
== cb_ptr
);
4000 return TEST_SUCCESS
;
4005 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4007 memcached_return_t rc
;
4008 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4009 my_realloc
, my_calloc
, NULL
);
4010 test_true(rc
== MEMCACHED_FAILURE
);
4012 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4013 my_realloc
, my_calloc
, NULL
);
4015 memcached_malloc_fn mem_malloc
;
4016 memcached_free_fn mem_free
;
4017 memcached_realloc_fn mem_realloc
;
4018 memcached_calloc_fn mem_calloc
;
4019 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4020 &mem_realloc
, &mem_calloc
);
4022 test_true(mem_malloc
== my_malloc
);
4023 test_true(mem_realloc
== my_realloc
);
4024 test_true(mem_calloc
== my_calloc
);
4025 test_true(mem_free
== my_free
);
4027 return TEST_SUCCESS
;
4030 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4033 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4034 memcached_hash_t hash
;
4035 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4036 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4039 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4040 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4042 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4044 if (hash
!= MEMCACHED_HASH_CRC
)
4045 return TEST_SKIPPED
;
4047 return TEST_SUCCESS
;
4050 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4053 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4054 memcached_hash_t hash
;
4055 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4056 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4059 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4060 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4062 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4064 if (hash
!= MEMCACHED_HASH_HSIEH
)
4065 return TEST_SKIPPED
;
4068 return TEST_SUCCESS
;
4071 static test_return_t
enable_cas(memcached_st
*memc
)
4073 unsigned int set
= 1;
4075 memcached_server_instance_st instance
=
4076 memcached_server_instance_by_position(memc
, 0);
4078 memcached_version(memc
);
4080 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4081 || instance
->minor_version
> 2)
4083 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4085 return TEST_SUCCESS
;
4088 return TEST_SKIPPED
;
4091 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4093 memcached_version(memc
);
4095 memcached_server_instance_st instance
=
4096 memcached_server_instance_by_position(memc
, 0);
4098 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4099 || instance
->minor_version
> 2)
4100 return TEST_SUCCESS
;
4102 return TEST_SKIPPED
;
4105 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4107 memcached_return_t rc
;
4110 memcached_servers_reset(memc
);
4112 if (stat("/tmp/memcached.socket", &buf
))
4113 return TEST_SKIPPED
;
4115 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4117 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4120 static test_return_t
pre_nodelay(memcached_st
*memc
)
4122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4123 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4125 return TEST_SUCCESS
;
4128 static test_return_t
pre_settimer(memcached_st
*memc
)
4130 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4131 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4133 return TEST_SUCCESS
;
4136 static test_return_t
poll_timeout(memcached_st
*memc
)
4142 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4144 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4146 test_true(timeout
== 100);
4148 return TEST_SUCCESS
;
4151 static test_return_t
noreply_test(memcached_st
*memc
)
4153 memcached_return_t ret
;
4154 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4155 test_true(ret
== MEMCACHED_SUCCESS
);
4156 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4157 test_true(ret
== MEMCACHED_SUCCESS
);
4158 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4159 test_true(ret
== MEMCACHED_SUCCESS
);
4160 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4161 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4162 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4164 for (int count
=0; count
< 5; ++count
)
4166 for (size_t x
= 0; x
< 100; ++x
)
4169 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4173 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4176 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4179 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4182 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4185 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4191 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4195 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4196 ** API and is _ONLY_ done this way to verify that the library works the
4197 ** way it is supposed to do!!!!
4200 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4202 memcached_server_instance_st instance
=
4203 memcached_server_instance_by_position(memc
, x
);
4204 no_msg
+=(int)(instance
->cursor_active
);
4207 test_true(no_msg
== 0);
4208 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4211 ** Now validate that all items was set properly!
4213 for (size_t x
= 0; x
< 100; ++x
)
4217 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4220 char* value
=memcached_get(memc
, key
, strlen(key
),
4221 &length
, &flags
, &ret
);
4222 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4225 case 0: /* FALLTHROUGH */
4226 case 1: /* FALLTHROUGH */
4228 test_true(strncmp(value
, key
, len
) == 0);
4229 test_true(len
== length
);
4232 test_true(length
== len
* 2);
4235 test_true(length
== len
* 3);
4245 /* Try setting an illegal cas value (should not return an error to
4246 * the caller (because we don't expect a return message from the server)
4248 const char* keys
[]= {"0"};
4249 size_t lengths
[]= {1};
4252 memcached_result_st results_obj
;
4253 memcached_result_st
*results
;
4254 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4255 test_true(ret
== MEMCACHED_SUCCESS
);
4257 results
= memcached_result_create(memc
, &results_obj
);
4259 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4261 test_true(ret
== MEMCACHED_SUCCESS
);
4262 uint64_t cas
= memcached_result_cas(results
);
4263 memcached_result_free(&results_obj
);
4265 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4266 test_true(ret
== MEMCACHED_SUCCESS
);
4269 * The item will have a new cas value, so try to set it again with the old
4270 * value. This should fail!
4272 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4273 test_true(ret
== MEMCACHED_SUCCESS
);
4274 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4275 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4276 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4279 return TEST_SUCCESS
;
4282 static test_return_t
analyzer_test(memcached_st
*memc
)
4284 memcached_return_t rc
;
4285 memcached_stat_st
*memc_stat
;
4286 memcached_analysis_st
*report
;
4288 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4289 test_true(rc
== MEMCACHED_SUCCESS
);
4290 test_true(memc_stat
);
4292 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4293 test_true(rc
== MEMCACHED_SUCCESS
);
4297 memcached_stat_free(NULL
, memc_stat
);
4299 return TEST_SUCCESS
;
4302 /* Count the objects */
4303 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4304 const char *key
__attribute__((unused
)),
4305 size_t key_length
__attribute__((unused
)),
4308 size_t *counter
= (size_t *)context
;
4310 *counter
= *counter
+ 1;
4312 return MEMCACHED_SUCCESS
;
4315 static test_return_t
dump_test(memcached_st
*memc
)
4317 memcached_return_t rc
;
4319 memcached_dump_fn callbacks
[1];
4320 test_return_t main_rc
;
4322 callbacks
[0]= &callback_dump_counter
;
4324 /* No support for Binary protocol yet */
4325 if (memc
->flags
.binary_protocol
)
4326 return TEST_SUCCESS
;
4328 main_rc
= set_test3(memc
);
4330 test_true (main_rc
== TEST_SUCCESS
);
4332 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4333 test_true(rc
== MEMCACHED_SUCCESS
);
4335 /* We may have more then 32 if our previous flush has not completed */
4336 test_true(counter
>= 32);
4338 return TEST_SUCCESS
;
4341 #ifdef HAVE_LIBMEMCACHEDUTIL
4342 static void* connection_release(void *arg
)
4345 memcached_pool_st
* pool
;
4350 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4354 static test_return_t
connection_pool_test(memcached_st
*memc
)
4356 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4357 test_true(pool
!= NULL
);
4358 memcached_st
* mmc
[10];
4359 memcached_return_t rc
;
4361 for (size_t x
= 0; x
< 10; ++x
)
4363 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4364 test_true(mmc
[x
] != NULL
);
4365 test_true(rc
== MEMCACHED_SUCCESS
);
4368 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4369 test_true(rc
== MEMCACHED_SUCCESS
);
4373 memcached_pool_st
* pool
;
4375 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4376 pthread_create(&tid
, NULL
, connection_release
, &item
);
4377 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4378 test_true(rc
== MEMCACHED_SUCCESS
);
4379 pthread_join(tid
, NULL
);
4380 test_true(mmc
[9] == item
.mmc
);
4381 const char *key
= "key";
4382 size_t keylen
= strlen(key
);
4384 // verify that I can do ops with all connections
4385 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4386 test_true(rc
== MEMCACHED_SUCCESS
);
4388 for (size_t x
= 0; x
< 10; ++x
)
4390 uint64_t number_value
;
4391 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4392 test_true(rc
== MEMCACHED_SUCCESS
);
4393 test_true(number_value
== (x
+1));
4397 for (size_t x
= 0; x
< 10; ++x
)
4399 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4403 /* verify that I can set behaviors on the pool when I don't have all
4404 * of the connections in the pool. It should however be enabled
4405 * when I push the item into the pool
4407 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4408 test_true(mmc
[0] != NULL
);
4410 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4411 test_true(rc
== MEMCACHED_SUCCESS
);
4413 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4414 test_true(mmc
[1] != NULL
);
4416 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4417 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4418 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4420 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4421 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4422 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4425 test_true(memcached_pool_destroy(pool
) == memc
);
4426 return TEST_SUCCESS
;
4429 static test_return_t
ping_test(memcached_st
*memc
)
4431 memcached_return_t rc
;
4432 memcached_server_instance_st instance
=
4433 memcached_server_instance_by_position(memc
, 0);
4435 // Test both the version that returns a code, and the one that does not.
4436 test_true(libmemcached_ping(memcached_server_name(instance
),
4437 memcached_server_port(instance
), NULL
));
4439 test_true(libmemcached_ping(memcached_server_name(instance
),
4440 memcached_server_port(instance
), &rc
));
4442 test_true(rc
== MEMCACHED_SUCCESS
);
4444 return TEST_SUCCESS
;
4448 static test_return_t
replication_set_test(memcached_st
*memc
)
4450 memcached_return_t rc
;
4451 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4452 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4454 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4455 test_true(rc
== MEMCACHED_SUCCESS
);
4458 ** We are using the quiet commands to store the replicas, so we need
4459 ** to ensure that all of them are processed before we can continue.
4460 ** In the test we go directly from storing the object to trying to
4461 ** receive the object from all of the different servers, so we
4462 ** could end up in a race condition (the memcached server hasn't yet
4463 ** processed the quiet command from the replication set when it process
4464 ** the request from the other client (created by the clone)). As a
4465 ** workaround for that we call memcached_quit to send the quit command
4466 ** to the server and wait for the response ;-) If you use the test code
4467 ** as an example for your own code, please note that you shouldn't need
4470 memcached_quit(memc
);
4473 ** "bubba" should now be stored on all of our servers. We don't have an
4474 ** easy to use API to address each individual server, so I'll just iterate
4475 ** through a bunch of "master keys" and I should most likely hit all of the
4478 for (int x
= 'a'; x
<= 'z'; ++x
)
4480 char key
[2]= { [0]= (char)x
};
4483 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4485 test_true(rc
== MEMCACHED_SUCCESS
);
4486 test_true(val
!= NULL
);
4490 memcached_free(memc_clone
);
4492 return TEST_SUCCESS
;
4495 static test_return_t
replication_get_test(memcached_st
*memc
)
4497 memcached_return_t rc
;
4500 * Don't do the following in your code. I am abusing the internal details
4501 * within the library, and this is not a supported interface.
4502 * This is to verify correct behavior in the library
4504 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4506 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4507 memcached_server_instance_st instance
=
4508 memcached_server_instance_by_position(memc_clone
, host
);
4510 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4512 for (int x
= 'a'; x
<= 'z'; ++x
)
4514 char key
[2]= { [0]= (char)x
};
4517 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4519 test_true(rc
== MEMCACHED_SUCCESS
);
4520 test_true(val
!= NULL
);
4524 memcached_free(memc_clone
);
4527 return TEST_SUCCESS
;
4530 static test_return_t
replication_mget_test(memcached_st
*memc
)
4532 memcached_return_t rc
;
4533 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4534 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4536 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4537 size_t len
[]= { 5, 4, 4, 4 };
4539 for (size_t x
= 0; x
< 4; ++x
)
4541 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4542 test_true(rc
== MEMCACHED_SUCCESS
);
4546 ** We are using the quiet commands to store the replicas, so we need
4547 ** to ensure that all of them are processed before we can continue.
4548 ** In the test we go directly from storing the object to trying to
4549 ** receive the object from all of the different servers, so we
4550 ** could end up in a race condition (the memcached server hasn't yet
4551 ** processed the quiet command from the replication set when it process
4552 ** the request from the other client (created by the clone)). As a
4553 ** workaround for that we call memcached_quit to send the quit command
4554 ** to the server and wait for the response ;-) If you use the test code
4555 ** as an example for your own code, please note that you shouldn't need
4558 memcached_quit(memc
);
4561 * Don't do the following in your code. I am abusing the internal details
4562 * within the library, and this is not a supported interface.
4563 * This is to verify correct behavior in the library
4565 memcached_result_st result_obj
;
4566 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4568 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4569 memcached_server_instance_st instance
=
4570 memcached_server_instance_by_position(new_clone
, host
);
4571 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4573 for (int x
= 'a'; x
<= 'z'; ++x
)
4575 char key
[2]= { [0]= (char)x
, [1]= 0 };
4577 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4578 test_true(rc
== MEMCACHED_SUCCESS
);
4580 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4584 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4588 test_true(hits
== 4);
4589 memcached_result_free(&result_obj
);
4592 memcached_free(new_clone
);
4595 memcached_free(memc_clone
);
4597 return TEST_SUCCESS
;
4600 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4602 memcached_result_st result_obj
;
4603 memcached_return_t rc
;
4604 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4605 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4606 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4608 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4609 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4611 for (size_t x
= 0; x
< 7; ++x
)
4613 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4614 test_true(rc
== MEMCACHED_SUCCESS
);
4617 memcached_quit(memc
);
4619 for (size_t x
= 0; x
< 7; ++x
)
4621 const char key
[2]= { [0]= (const char)x
};
4623 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4624 test_true(rc
== MEMCACHED_SUCCESS
);
4626 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4630 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4634 test_true(hits
== 7);
4635 memcached_result_free(&result_obj
);
4637 memcached_free(memc_clone
);
4638 return TEST_SUCCESS
;
4641 static test_return_t
replication_delete_test(memcached_st
*memc
)
4643 memcached_return_t rc
;
4644 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4645 /* Delete the items from all of the servers except 1 */
4646 uint64_t repl
= memcached_behavior_get(memc
,
4647 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4648 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4650 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4651 size_t len
[]= { 5, 4, 4, 4 };
4653 for (size_t x
= 0; x
< 4; ++x
)
4655 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4656 test_true(rc
== MEMCACHED_SUCCESS
);
4660 * Don't do the following in your code. I am abusing the internal details
4661 * within the library, and this is not a supported interface.
4662 * This is to verify correct behavior in the library
4664 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4665 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4667 memcached_server_instance_st instance
=
4668 memcached_server_instance_by_position(memc_clone
, x
);
4670 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4671 if (++hash
== memc_clone
->number_of_hosts
)
4675 memcached_result_st result_obj
;
4676 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4678 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4680 const char key
[2]= { [0]= (const char)x
};
4682 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4683 test_true(rc
== MEMCACHED_SUCCESS
);
4685 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4689 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4693 test_true(hits
== 4);
4694 memcached_result_free(&result_obj
);
4697 memcached_free(memc_clone
);
4699 return TEST_SUCCESS
;
4703 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4707 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4708 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4709 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4710 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4711 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4712 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4713 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4714 #ifdef HAVE_HSIEH_HASH
4715 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4717 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4718 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4719 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4721 return TEST_SUCCESS
;
4725 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4727 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4728 #ifdef HAVE_HSIEH_HASH
4729 expected_rc
= MEMCACHED_SUCCESS
;
4731 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4732 (uint64_t)MEMCACHED_HASH_HSIEH
);
4733 test_true(rc
== expected_rc
);
4735 return TEST_SUCCESS
;
4738 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4743 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4747 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4748 test_true(one_at_a_time_values
[x
] == hash_val
);
4751 return TEST_SUCCESS
;
4754 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4759 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4763 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4764 test_true(md5_values
[x
] == hash_val
);
4767 return TEST_SUCCESS
;
4770 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4775 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4779 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4780 test_true(crc_values
[x
] == hash_val
);
4783 return TEST_SUCCESS
;
4786 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4791 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4795 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4796 test_true(fnv1_64_values
[x
] == hash_val
);
4799 return TEST_SUCCESS
;
4802 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4807 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4811 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4812 test_true(fnv1a_64_values
[x
] == hash_val
);
4815 return TEST_SUCCESS
;
4818 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4824 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4828 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4829 test_true(fnv1_32_values
[x
] == hash_val
);
4832 return TEST_SUCCESS
;
4835 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4840 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4844 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4845 test_true(fnv1a_32_values
[x
] == hash_val
);
4848 return TEST_SUCCESS
;
4851 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4856 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4860 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4861 test_true(hsieh_values
[x
] == hash_val
);
4864 return TEST_SUCCESS
;
4867 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4869 #ifdef WORDS_BIGENDIAN
4870 return TEST_SKIPPED
;
4875 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4879 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4880 test_true(murmur_values
[x
] == hash_val
);
4883 return TEST_SUCCESS
;
4887 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4893 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4897 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4898 test_true(jenkins_values
[x
] == hash_val
);
4901 return TEST_SUCCESS
;
4904 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4907 return libhashkit_md5(string
, string_length
);
4910 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4913 return libhashkit_crc32(string
, string_length
);
4916 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4920 const hashkit_st
*kit
;
4922 hashkit_return_t hash_rc
;
4924 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};
4925 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};
4927 kit
= memcached_get_hashkit(memc
);
4929 hashkit_clone(&new_kit
, kit
);
4930 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4931 test_true(hash_rc
== HASHKIT_SUCCESS
);
4933 memcached_set_hashkit(memc
, &new_kit
);
4936 Verify Setting the hash.
4938 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4942 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4943 test_true(md5_values
[x
] == hash_val
);
4948 Now check memcached_st.
4950 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4954 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4955 test_true(md5_hosts
[x
] == hash_val
);
4958 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4959 test_true(hash_rc
== HASHKIT_SUCCESS
);
4961 memcached_set_hashkit(memc
, &new_kit
);
4964 Verify Setting the hash.
4966 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4970 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4971 test_true(crc_values
[x
] == hash_val
);
4974 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4978 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4979 test_true(crc_hosts
[x
] == hash_val
);
4982 return TEST_SUCCESS
;
4986 Test case adapted from John Gorman <johngorman2@gmail.com>
4988 We are testing the error condition when we connect to a server via memcached_get()
4989 but find that the server is not available.
4991 static test_return_t
memcached_get_MEMCACHED_SOME_ERRORS(memcached_st
*memc
)
4994 memcached_st
*tl_memc_h
;
4995 memcached_server_st
*servers
;
4997 const char *key
= "MemcachedLives";
5000 memcached_return rc
;
5004 tl_memc_h
= memcached_create(NULL
);
5005 servers
= memcached_servers_parse("localhost:9898"); // This server should not exist
5006 memcached_server_push(tl_memc_h
, servers
);
5007 memcached_server_list_free(servers
);
5009 // See if memcached is reachable.
5010 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5015 test_true(value
); // Pointer won't be zero so this is fine.
5018 test_true(len
== 0);
5019 test_true(rc
== MEMCACHED_SOME_ERRORS
);
5021 return TEST_SUCCESS
;
5025 We connect to a server which exists, but search for a key that does not exist.
5027 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5029 const char *key
= "MemcachedKeyNotEXIST";
5032 memcached_return rc
;
5035 // See if memcached is reachable.
5036 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5041 test_true(value
); // Pointer won't be zero so this is fine.
5044 test_true(len
== 0);
5045 test_true(rc
== MEMCACHED_NOTFOUND
);
5047 return TEST_SUCCESS
;
5051 Test case adapted from John Gorman <johngorman2@gmail.com>
5053 We are testing the error condition when we connect to a server via memcached_get_by_key()
5054 but find that the server is not available.
5056 static test_return_t
memcached_get_by_key_MEMCACHED_SOME_ERRORS(memcached_st
*memc
)
5059 memcached_st
*tl_memc_h
;
5060 memcached_server_st
*servers
;
5062 const char *key
= "MemcachedLives";
5065 memcached_return rc
;
5069 tl_memc_h
= memcached_create(NULL
);
5070 servers
= memcached_servers_parse("localhost:9898"); // This server should not exist
5071 memcached_server_push(tl_memc_h
, servers
);
5072 memcached_server_list_free(servers
);
5074 // See if memcached is reachable.
5075 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5080 test_true(value
); // Pointer won't be zero so this is fine.
5083 test_true(len
== 0);
5084 test_true(rc
== MEMCACHED_SOME_ERRORS
);
5086 return TEST_SUCCESS
;
5090 We connect to a server which exists, but search for a key that does not exist.
5092 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5094 const char *key
= "MemcachedKeyNotEXIST";
5097 memcached_return rc
;
5100 // See if memcached is reachable.
5101 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5106 test_true(value
); // Pointer won't be zero so this is fine.
5109 test_true(len
== 0);
5110 test_true(rc
== MEMCACHED_NOTFOUND
);
5112 return TEST_SUCCESS
;
5116 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5118 memcached_return_t rc
;
5121 memcached_server_st
*server_pool
;
5126 memc
= memcached_create(NULL
);
5129 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5130 test_true(rc
== MEMCACHED_SUCCESS
);
5132 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5133 test_true(value
== 1);
5135 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5136 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5139 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");
5140 memcached_server_push(memc
, server_pool
);
5142 /* verify that the server list was parsed okay. */
5143 test_true(memcached_server_count(memc
) == 8);
5144 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5145 test_true(server_pool
[0].port
== 11211);
5146 test_true(server_pool
[0].weight
== 600);
5147 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5148 test_true(server_pool
[2].port
== 11211);
5149 test_true(server_pool
[2].weight
== 200);
5150 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5151 test_true(server_pool
[7].port
== 11211);
5152 test_true(server_pool
[7].weight
== 100);
5154 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5155 * us test the boundary wraparound.
5157 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5159 /* verify the standard ketama set. */
5160 for (x
= 0; x
< 99; x
++)
5162 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5163 memcached_server_instance_st instance
=
5164 memcached_server_instance_by_position(memc
, server_idx
);
5165 const char *hostname
= memcached_server_name(instance
);
5167 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5170 memcached_server_list_free(server_pool
);
5171 memcached_free(memc
);
5173 return TEST_SUCCESS
;
5176 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5178 memcached_return_t rc
;
5181 memcached_server_st
*server_pool
;
5186 memc
= memcached_create(NULL
);
5189 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5190 test_true(rc
== MEMCACHED_SUCCESS
);
5192 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5193 test_true(value
== 1);
5195 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5196 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5198 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");
5199 memcached_server_push(memc
, server_pool
);
5201 /* verify that the server list was parsed okay. */
5202 test_true(memcached_server_count(memc
) == 8);
5203 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5204 test_true(server_pool
[0].port
== 11211);
5205 test_true(server_pool
[0].weight
== 600);
5206 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5207 test_true(server_pool
[2].port
== 11211);
5208 test_true(server_pool
[2].weight
== 200);
5209 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5210 test_true(server_pool
[7].port
== 11211);
5211 test_true(server_pool
[7].weight
== 100);
5213 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5214 * us test the boundary wraparound.
5216 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5218 /* verify the standard ketama set. */
5219 for (x
= 0; x
< 99; x
++)
5221 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5223 memcached_server_instance_st instance
=
5224 memcached_server_instance_by_position(memc
, server_idx
);
5226 const char *hostname
= memcached_server_name(instance
);
5228 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5231 memcached_server_list_free(server_pool
);
5232 memcached_free(memc
);
5234 return TEST_SUCCESS
;
5237 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5239 test_return_t test_rc
;
5240 test_rc
= pre_binary(memc
);
5242 if (test_rc
!= TEST_SUCCESS
)
5245 memcached_return_t ret
;
5246 const char *key
= "regression_bug_434484";
5247 size_t keylen
= strlen(key
);
5249 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5250 test_true(ret
== MEMCACHED_NOTSTORED
);
5252 size_t size
= 2048 * 1024;
5253 void *data
= calloc(1, size
);
5254 test_true(data
!= NULL
);
5255 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5256 test_true(ret
== MEMCACHED_E2BIG
);
5259 return TEST_SUCCESS
;
5262 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5264 test_return_t test_rc
;
5265 test_rc
= pre_binary(memc
);
5267 if (test_rc
!= TEST_SUCCESS
)
5270 memcached_return_t rc
;
5272 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5275 * I only want to hit only _one_ server so I know the number of requests I'm
5276 * sending in the pipleine to the server. Let's try to do a multiget of
5277 * 1024 (that should satisfy most users don't you think?). Future versions
5278 * will include a mget_execute function call if you need a higher number.
5280 uint32_t number_of_hosts
= memcached_server_count(memc
);
5281 memc
->number_of_hosts
= 1;
5282 const size_t max_keys
= 1024;
5283 char **keys
= calloc(max_keys
, sizeof(char*));
5284 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5286 for (size_t x
= 0; x
< max_keys
; ++x
)
5290 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5292 test_true(keys
[x
] != NULL
);
5296 * Run two times.. the first time we should have 100% cache miss,
5297 * and the second time we should have 100% cache hits
5299 for (size_t y
= 0; y
< 2; y
++)
5301 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5302 test_true(rc
== MEMCACHED_SUCCESS
);
5303 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5307 /* The first iteration should give me a 100% cache miss. verify that*/
5308 char blob
[1024]= { 0 };
5310 test_true(counter
== 0);
5312 for (size_t x
= 0; x
< max_keys
; ++x
)
5314 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5315 blob
, sizeof(blob
), 0, 0);
5316 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5321 /* Verify that we received all of the key/value pairs */
5322 test_true(counter
== max_keys
);
5326 /* Release allocated resources */
5327 for (size_t x
= 0; x
< max_keys
; ++x
)
5334 memc
->number_of_hosts
= number_of_hosts
;
5336 return TEST_SUCCESS
;
5339 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5341 memcached_return_t rc
;
5342 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5343 test_true(rc
== MEMCACHED_SUCCESS
);
5345 return regression_bug_434843(memc
);
5348 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5350 memcached_return_t rc
;
5351 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5352 test_true(rc
== MEMCACHED_SUCCESS
);
5354 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5355 test_true(rc
== MEMCACHED_SUCCESS
);
5356 test_true(bytes
!= NULL
);
5357 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5359 test_true(rc
== MEMCACHED_SUCCESS
);
5360 test_true(bytes_read
!= NULL
);
5362 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5363 "bytes_written", &rc
);
5364 test_true(rc
== MEMCACHED_SUCCESS
);
5365 test_true(bytes_written
!= NULL
);
5367 test_true(strcmp(bytes
, bytes_read
) != 0);
5368 test_true(strcmp(bytes
, bytes_written
) != 0);
5370 /* Release allocated resources */
5373 free(bytes_written
);
5374 memcached_stat_free(NULL
, memc_stat
);
5376 return TEST_SUCCESS
;
5380 * The test case isn't obvious so I should probably document why
5381 * it works the way it does. Bug 442914 was caused by a bug
5382 * in the logic in memcached_purge (it did not handle the case
5383 * where the number of bytes sent was equal to the watermark).
5384 * In this test case, create messages so that we hit that case
5385 * and then disable noreply mode and issue a new command to
5386 * verify that it isn't stuck. If we change the format for the
5387 * delete command or the watermarks, we need to update this
5390 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5392 memcached_return_t rc
;
5393 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5394 test_true(rc
== MEMCACHED_SUCCESS
);
5395 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5397 uint32_t number_of_hosts
= memcached_server_count(memc
);
5398 memc
->number_of_hosts
= 1;
5403 for (uint32_t x
= 0; x
< 250; ++x
)
5405 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5406 rc
= memcached_delete(memc
, k
, len
, 0);
5407 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5410 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5413 rc
= memcached_delete(memc
, k
, len
, 0);
5414 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5416 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5417 test_true(rc
== MEMCACHED_SUCCESS
);
5418 rc
= memcached_delete(memc
, k
, len
, 0);
5419 test_true(rc
== MEMCACHED_NOTFOUND
);
5421 memc
->number_of_hosts
= number_of_hosts
;
5423 return TEST_SUCCESS
;
5426 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5428 memcached_server_instance_st instance_one
;
5429 memcached_server_instance_st instance_two
;
5431 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5432 return TEST_SKIPPED
;
5434 memcached_return_t rc
;
5436 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5437 test_true(rc
== MEMCACHED_SUCCESS
);
5439 const size_t max_keys
= 100;
5440 char **keys
= calloc(max_keys
, sizeof(char*));
5441 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5443 for (size_t x
= 0; x
< max_keys
; ++x
)
5447 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5449 test_true(keys
[x
] != NULL
);
5450 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5451 test_true(rc
== MEMCACHED_SUCCESS
);
5455 ** We are using the quiet commands to store the replicas, so we need
5456 ** to ensure that all of them are processed before we can continue.
5457 ** In the test we go directly from storing the object to trying to
5458 ** receive the object from all of the different servers, so we
5459 ** could end up in a race condition (the memcached server hasn't yet
5460 ** processed the quiet command from the replication set when it process
5461 ** the request from the other client (created by the clone)). As a
5462 ** workaround for that we call memcached_quit to send the quit command
5463 ** to the server and wait for the response ;-) If you use the test code
5464 ** as an example for your own code, please note that you shouldn't need
5467 memcached_quit(memc
);
5469 /* Verify that all messages are stored, and we didn't stuff too much
5472 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5473 test_true(rc
== MEMCACHED_SUCCESS
);
5476 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5477 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5478 /* Verify that we received all of the key/value pairs */
5479 test_true(counter
== max_keys
);
5481 memcached_quit(memc
);
5483 * Don't do the following in your code. I am abusing the internal details
5484 * within the library, and this is not a supported interface.
5485 * This is to verify correct behavior in the library. Fake that two servers
5488 instance_one
= memcached_server_instance_by_position(memc
, 0);
5489 instance_two
= memcached_server_instance_by_position(memc
, 2);
5490 in_port_t port0
= instance_one
->port
;
5491 in_port_t port2
= instance_two
->port
;
5493 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5494 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5496 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5497 test_true(rc
== MEMCACHED_SUCCESS
);
5500 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5501 test_true(counter
== (unsigned int)max_keys
);
5503 /* restore the memc handle */
5504 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5505 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5507 memcached_quit(memc
);
5509 /* Remove half of the objects */
5510 for (size_t x
= 0; x
< max_keys
; ++x
)
5514 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5515 test_true(rc
== MEMCACHED_SUCCESS
);
5519 memcached_quit(memc
);
5520 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5521 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5523 /* now retry the command, this time we should have cache misses */
5524 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5525 test_true(rc
== MEMCACHED_SUCCESS
);
5528 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5529 test_true(counter
== (unsigned int)(max_keys
>> 1));
5531 /* Release allocated resources */
5532 for (size_t x
= 0; x
< max_keys
; ++x
)
5539 /* restore the memc handle */
5540 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5541 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5543 return TEST_SUCCESS
;
5546 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5548 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5549 test_true(memc_clone
!= NULL
);
5550 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5552 memcached_server_instance_st instance
=
5553 memcached_server_instance_by_position(memc_clone
, 0);
5555 if (instance
->major_version
> 1 ||
5556 (instance
->major_version
== 1 &&
5557 instance
->minor_version
> 2))
5559 /* Binary protocol doesn't support deferred delete */
5560 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5561 test_true(bin_clone
!= NULL
);
5562 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5563 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5564 memcached_free(bin_clone
);
5566 memcached_quit(memc_clone
);
5568 /* If we know the server version, deferred delete should fail
5569 * with invalid arguments */
5570 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5572 /* If we don't know the server version, we should get a protocol error */
5573 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5575 /* but there is a bug in some of the memcached servers (1.4) that treats
5576 * the counter as noreply so it doesn't send the proper error message
5578 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5580 /* And buffered mode should be disabled and we should get protocol error */
5581 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5582 rc
= memcached_delete(memc
, "foo", 3, 1);
5583 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5585 /* Same goes for noreply... */
5586 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5587 rc
= memcached_delete(memc
, "foo", 3, 1);
5588 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5590 /* but a normal request should go through (and be buffered) */
5591 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5592 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5594 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5595 /* unbuffered noreply should be success */
5596 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5597 /* unbuffered with reply should be not found... */
5598 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5599 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5602 memcached_free(memc_clone
);
5603 return TEST_SUCCESS
;
5607 /* Test memcached_server_get_last_disconnect
5608 * For a working server set, shall be NULL
5609 * For a set of non existing server, shall not be NULL
5611 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5613 memcached_return_t rc
;
5614 memcached_server_instance_st disconnected_server
;
5616 /* With the working set of server */
5617 const char *key
= "marmotte";
5618 const char *value
= "milka";
5620 memcached_reset_last_disconnected_server(memc
);
5621 rc
= memcached_set(memc
, key
, strlen(key
),
5622 value
, strlen(value
),
5623 (time_t)0, (uint32_t)0);
5624 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5626 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5627 test_true(disconnected_server
== NULL
);
5629 /* With a non existing server */
5631 memcached_server_st
*servers
;
5633 const char *server_list
= "localhost:9";
5635 servers
= memcached_servers_parse(server_list
);
5637 mine
= memcached_create(NULL
);
5638 rc
= memcached_server_push(mine
, servers
);
5639 test_true(rc
== MEMCACHED_SUCCESS
);
5640 memcached_server_list_free(servers
);
5643 rc
= memcached_set(mine
, key
, strlen(key
),
5644 value
, strlen(value
),
5645 (time_t)0, (uint32_t)0);
5646 test_true(rc
!= MEMCACHED_SUCCESS
);
5648 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5649 if (disconnected_server
== NULL
)
5651 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5654 test_true(disconnected_server
!= NULL
);
5655 test_true(memcached_server_port(disconnected_server
)== 9);
5656 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5658 memcached_quit(mine
);
5659 memcached_free(mine
);
5661 return TEST_SUCCESS
;
5664 static test_return_t
test_verbosity(memcached_st
*memc
)
5666 memcached_verbosity(memc
, 3);
5668 return TEST_SUCCESS
;
5671 static test_return_t
test_server_failure(memcached_st
*memc
)
5673 memcached_st
*local_memc
;
5674 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5676 local_memc
= memcached_create(NULL
);
5678 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5679 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5681 uint32_t server_count
= memcached_server_count(local_memc
);
5683 test_true(server_count
== 1);
5685 // Disable the server
5686 instance
= memcached_server_instance_by_position(local_memc
, 0);
5687 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5689 memcached_return_t rc
;
5690 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5692 (time_t)0, (uint32_t)0);
5693 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5695 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5696 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5698 (time_t)0, (uint32_t)0);
5699 test_true(rc
== MEMCACHED_SUCCESS
);
5702 memcached_free(local_memc
);
5704 return TEST_SUCCESS
;
5707 static test_return_t
test_cull_servers(memcached_st
*memc
)
5709 uint32_t count
= memcached_server_count(memc
);
5711 // Do not do this in your code, it is not supported.
5712 memc
->servers
[1].state
.is_dead
= true;
5713 memc
->state
.is_time_for_rebuild
= true;
5715 uint32_t new_count
= memcached_server_count(memc
);
5716 test_true(count
== new_count
);
5719 test_true(count
== new_count
+ 1 );
5722 return TEST_SUCCESS
;
5726 * This test ensures that the failure counter isn't incremented during
5727 * normal termination of the memcached instance.
5729 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5731 memcached_return_t rc
;
5732 memcached_server_instance_st instance
;
5734 /* Set value to force connection to the server */
5735 const char *key
= "marmotte";
5736 const char *value
= "milka";
5739 * Please note that I'm abusing the internal structures in libmemcached
5740 * in a non-portable way and you shouldn't be doing this. I'm only
5741 * doing this in order to verify that the library works the way it should
5743 uint32_t number_of_hosts
= memcached_server_count(memc
);
5744 memc
->number_of_hosts
= 1;
5746 /* Ensure that we are connected to the server by setting a value */
5747 rc
= memcached_set(memc
, key
, strlen(key
),
5748 value
, strlen(value
),
5749 (time_t)0, (uint32_t)0);
5750 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5753 instance
= memcached_server_instance_by_position(memc
, 0);
5754 /* The test is to see that the memcached_quit doesn't increase the
5755 * the server failure conter, so let's ensure that it is zero
5756 * before sending quit
5758 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5760 memcached_quit(memc
);
5762 /* Verify that it memcached_quit didn't increment the failure counter
5763 * Please note that this isn't bullet proof, because an error could
5766 test_true(instance
->server_failure_counter
== 0);
5768 /* restore the instance */
5769 memc
->number_of_hosts
= number_of_hosts
;
5771 return TEST_SUCCESS
;
5778 * Test that ensures mget_execute does not end into recursive calls that finally fails
5780 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5782 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5783 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5784 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5785 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5786 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5789 return TEST_SKIPPED
; // My MAC can't handle this test
5793 * I only want to hit _one_ server so I know the number of requests I'm
5794 * sending in the pipeline.
5796 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5797 memc
->number_of_hosts
= 1;
5798 size_t max_keys
= 20480;
5801 char **keys
= calloc(max_keys
, sizeof(char*));
5802 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5804 /* First add all of the items.. */
5806 char blob
[1024]= { 0 };
5807 memcached_return rc
;
5808 for (size_t x
= 0; x
< max_keys
; ++x
)
5811 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5813 assert(keys
[x
] != NULL
);
5814 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5816 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5818 break; // We are out of business
5821 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5823 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5826 sleep(1);// We will try to sleep
5829 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5831 // We failed to send everything.
5836 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5839 /* Try to get all of them with a large multiget */
5841 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5842 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5843 (size_t)max_keys
, callbacks
, &counter
, 1);
5845 assert(rc
== MEMCACHED_SUCCESS
);
5846 char* the_value
= NULL
;
5847 char the_key
[MEMCACHED_MAX_KEY
];
5848 size_t the_key_length
;
5849 size_t the_value_length
;
5853 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5855 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5861 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5864 assert(rc
== MEMCACHED_END
);
5866 /* Verify that we got all of the items */
5867 assert(counter
== max_keys
);
5870 /* Release all allocated resources */
5871 for (size_t x
= 0; x
< max_keys
; ++x
)
5878 memc
->number_of_hosts
= number_of_hosts
;
5880 return TEST_SUCCESS
;
5883 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5887 memcached_st
*memc
= memcached_create(NULL
);
5889 memcached_server_add(memc
, "10.2.3.4", 11211);
5891 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5892 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5893 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5894 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5895 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5896 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5898 memcached_return_t rc
;
5902 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5904 test_true(rc
== MEMCACHED_TIMEOUT
);
5906 memcached_free(memc
);
5908 return TEST_SUCCESS
;
5911 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5913 fprintf(stderr
, "Iteration #%u: ", it
);
5915 if(error
== MEMCACHED_ERRNO
)
5917 fprintf(stderr
, "system error %d from %s: %s\n",
5918 errno
, what
, strerror(errno
));
5922 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5923 memcached_strerror(mc
, error
));
5927 #define TEST_CONSTANT_CREATION 200
5929 static test_return_t
regression_bug_(memcached_st
*memc
)
5931 const char *remote_server
;
5934 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5936 return TEST_SKIPPED
;
5939 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5941 memcached_st
* mc
= memcached_create(NULL
);
5942 memcached_return rc
;
5944 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5945 if (rc
!= MEMCACHED_SUCCESS
)
5947 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5950 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5951 if (rc
!= MEMCACHED_SUCCESS
)
5953 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5956 rc
= memcached_server_add(mc
, remote_server
, 0);
5957 if (rc
!= MEMCACHED_SUCCESS
)
5959 memcached_die(mc
, rc
, "memcached_server_add", x
);
5962 const char *set_key
= "akey";
5963 const size_t set_key_len
= strlen(set_key
);
5964 const char *set_value
= "a value";
5965 const size_t set_value_len
= strlen(set_value
);
5967 if (rc
== MEMCACHED_SUCCESS
)
5971 size_t get_value_len
;
5973 uint32_t get_value_flags
;
5975 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5976 &get_value_flags
, &rc
);
5977 if (rc
!= MEMCACHED_SUCCESS
)
5979 memcached_die(mc
, rc
, "memcached_get", x
);
5985 (get_value_len
!= set_value_len
5986 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5988 fprintf(stderr
, "Values don't match?\n");
5989 rc
= MEMCACHED_FAILURE
;
5995 rc
= memcached_set(mc
,
5996 set_key
, set_key_len
,
5997 set_value
, set_value_len
,
6001 if (rc
!= MEMCACHED_SUCCESS
)
6003 memcached_die(mc
, rc
, "memcached_set", x
);
6010 if (rc
!= MEMCACHED_SUCCESS
)
6016 return TEST_SUCCESS
;
6020 * Test that the sasl authentication works. We cannot use the default
6021 * pool of servers, because that would require that all servers we want
6022 * to test supports SASL authentication, and that they use the default
6025 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6027 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6028 memcached_return_t rc
;
6030 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6031 test_true(rc
== MEMCACHED_SUCCESS
);
6032 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6033 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6034 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6035 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6036 memcached_quit(memc
);
6038 rc
= memcached_set_sasl_auth_data(memc
,
6039 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6040 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6041 test_true(rc
== MEMCACHED_SUCCESS
);
6043 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6044 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6045 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6047 memcached_quit(memc
);
6048 return TEST_SUCCESS
;
6051 return TEST_FAILURE
;
6055 /* Clean the server before beginning testing */
6057 {"flush", 0, (test_callback_fn
)flush_test
},
6058 {"init", 0, (test_callback_fn
)init_test
},
6059 {"allocation", 0, (test_callback_fn
)allocation_test
},
6060 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6061 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6062 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6063 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6064 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6065 {"clone_test", 0, (test_callback_fn
)clone_test
},
6066 {"connection_test", 0, (test_callback_fn
)connection_test
},
6067 {"callback_test", 0, (test_callback_fn
)callback_test
},
6068 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6069 {"error", 0, (test_callback_fn
)error_test
},
6070 {"set", 0, (test_callback_fn
)set_test
},
6071 {"set2", 0, (test_callback_fn
)set_test2
},
6072 {"set3", 0, (test_callback_fn
)set_test3
},
6073 {"dump", 1, (test_callback_fn
)dump_test
},
6074 {"add", 1, (test_callback_fn
)add_test
},
6075 {"replace", 1, (test_callback_fn
)replace_test
},
6076 {"delete", 1, (test_callback_fn
)delete_test
},
6077 {"get", 1, (test_callback_fn
)get_test
},
6078 {"get2", 0, (test_callback_fn
)get_test2
},
6079 {"get3", 0, (test_callback_fn
)get_test3
},
6080 {"get4", 0, (test_callback_fn
)get_test4
},
6081 {"partial mget", 0, (test_callback_fn
)get_test5
},
6082 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6083 {"increment", 0, (test_callback_fn
)increment_test
},
6084 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6085 {"decrement", 0, (test_callback_fn
)decrement_test
},
6086 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6087 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6088 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6089 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6090 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6091 {"quit", 0, (test_callback_fn
)quit_test
},
6092 {"mget", 1, (test_callback_fn
)mget_test
},
6093 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6094 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6095 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6096 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6097 {"mget_end", 0, (test_callback_fn
)mget_end
},
6098 {"get_stats", 0, (test_callback_fn
)get_stats
},
6099 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6100 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6101 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6102 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6103 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6104 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6105 {"read_through", 1, (test_callback_fn
)read_through
},
6106 {"delete_through", 1, (test_callback_fn
)delete_through
},
6107 {"noreply", 1, (test_callback_fn
)noreply_test
},
6108 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6109 #ifdef HAVE_LIBMEMCACHEDUTIL
6110 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6111 {"ping", 1, (test_callback_fn
)ping_test
},
6113 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6114 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6115 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6116 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6120 test_st behavior_tests
[] ={
6121 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6122 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6123 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6124 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6128 test_st regression_binary_vs_block
[] ={
6129 {"block add", 1, (test_callback_fn
)block_add_regression
},
6130 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6134 test_st async_tests
[] ={
6135 {"add", 1, (test_callback_fn
)add_wrapper
},
6139 test_st string_tests
[] ={
6140 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6141 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6142 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6143 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6144 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6145 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6146 {0, 0, (test_callback_fn
)0}
6149 test_st result_tests
[] ={
6150 {"result static", 0, (test_callback_fn
)result_static
},
6151 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6152 {0, 0, (test_callback_fn
)0}
6155 test_st version_1_2_3
[] ={
6156 {"append", 0, (test_callback_fn
)append_test
},
6157 {"prepend", 0, (test_callback_fn
)prepend_test
},
6158 {"cas", 0, (test_callback_fn
)cas_test
},
6159 {"cas2", 0, (test_callback_fn
)cas2_test
},
6160 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6161 {0, 0, (test_callback_fn
)0}
6164 test_st user_tests
[] ={
6165 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6166 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6167 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6168 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6169 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6170 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6171 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6172 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6173 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6174 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6175 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6176 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6177 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6178 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6179 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6180 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6183 ** It seems to be something weird with the character sets..
6184 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6185 ** guess I need to find out how this is supposed to work.. Perhaps I need
6186 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6187 ** so just disable the code for now...).
6189 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6191 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6192 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6193 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6194 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6195 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6196 {0, 0, (test_callback_fn
)0}
6199 test_st replication_tests
[]= {
6200 {"set", 1, (test_callback_fn
)replication_set_test
},
6201 {"get", 0, (test_callback_fn
)replication_get_test
},
6202 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6203 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6204 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6205 {0, 0, (test_callback_fn
)0}
6209 * The following test suite is used to verify that we don't introduce
6210 * regression bugs. If you want more information about the bug / test,
6211 * you should look in the bug report at
6212 * http://bugs.launchpad.net/libmemcached
6214 test_st regression_tests
[]= {
6215 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6216 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6217 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6218 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6219 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6220 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6221 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6222 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6223 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6224 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6225 {0, 0, (test_callback_fn
)0}
6228 test_st sasl_auth_tests
[]= {
6229 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6230 {0, 0, (test_callback_fn
)0}
6233 test_st ketama_compatibility
[]= {
6234 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6235 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6236 {0, 0, (test_callback_fn
)0}
6239 test_st generate_tests
[] ={
6240 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6241 {"generate_data", 1, (test_callback_fn
)generate_data
},
6242 {"get_read", 0, (test_callback_fn
)get_read
},
6243 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6244 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6245 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6246 {"generate_data", 1, (test_callback_fn
)generate_data
},
6247 {"mget_read", 0, (test_callback_fn
)mget_read
},
6248 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6249 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6250 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6251 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6252 {"generate_data", 1, (test_callback_fn
)generate_data
},
6253 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6254 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6255 {0, 0, (test_callback_fn
)0}
6258 test_st consistent_tests
[] ={
6259 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6260 {"generate_data", 1, (test_callback_fn
)generate_data
},
6261 {"get_read", 0, (test_callback_fn
)get_read_count
},
6262 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6263 {0, 0, (test_callback_fn
)0}
6266 test_st consistent_weighted_tests
[] ={
6267 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6268 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6269 {"get_read", 0, (test_callback_fn
)get_read_count
},
6270 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6271 {0, 0, (test_callback_fn
)0}
6274 test_st hsieh_availability
[] ={
6275 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6276 {0, 0, (test_callback_fn
)0}
6280 test_st hash_sanity
[] ={
6281 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6282 {0, 0, (test_callback_fn
)0}
6286 test_st ketama_auto_eject_hosts
[] ={
6287 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6288 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6289 {0, 0, (test_callback_fn
)0}
6292 test_st hash_tests
[] ={
6293 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6294 {"md5", 0, (test_callback_fn
)md5_run
},
6295 {"crc", 0, (test_callback_fn
)crc_run
},
6296 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6297 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6298 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6299 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6300 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6301 {"murmur", 0, (test_callback_fn
)murmur_run
},
6302 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6303 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6304 {0, 0, (test_callback_fn
)0}
6307 test_st error_conditions
[] ={
6308 {"memcached_get_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn
)memcached_get_MEMCACHED_SOME_ERRORS
},
6309 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6310 {"memcached_get_by_key_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_SOME_ERRORS
},
6311 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6312 {0, 0, (test_callback_fn
)0}
6315 collection_st collection
[] ={
6317 {"hash_sanity", 0, 0, hash_sanity
},
6319 {"hsieh_availability", 0, 0, hsieh_availability
},
6320 {"block", 0, 0, tests
},
6321 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6322 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6323 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6324 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6325 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6326 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6327 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6328 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6329 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6330 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6331 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6332 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6333 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6334 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6335 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6336 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6337 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6338 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6339 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6340 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6341 #ifdef MEMCACHED_ENABLE_DEPRECATED
6342 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6344 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6345 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6346 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6347 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6348 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6349 {"string", 0, 0, string_tests
},
6350 {"result", 0, 0, result_tests
},
6351 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6352 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6353 {"user", 0, 0, user_tests
},
6354 {"generate", 0, 0, generate_tests
},
6355 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6356 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6357 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6358 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6359 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6360 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6361 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6363 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6364 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6365 {"consistent_not", 0, 0, consistent_tests
},
6366 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6367 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6368 {"ketama_compat", 0, 0, ketama_compatibility
},
6369 {"test_hashes", 0, 0, hash_tests
},
6370 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6371 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6372 {"regression", 0, 0, regression_tests
},
6373 {"behaviors", 0, 0, behavior_tests
},
6374 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6375 {"error_conditions", 0, 0, error_conditions
},
6379 #define SERVERS_TO_CREATE 5
6381 #include "libmemcached_world.h"
6383 void get_world(world_st
*world
)
6385 world
->collections
= collection
;
6387 world
->create
= (test_callback_create_fn
)world_create
;
6388 world
->destroy
= (test_callback_fn
)world_destroy
;
6390 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6391 world
->test
.flush
= (test_callback_fn
)world_flush
;
6392 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6393 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6394 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6396 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6397 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6399 world
->runner
= &defualt_libmemcached_runner
;