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
;
3596 memc_clone
= memcached_clone(NULL
, memc
);
3597 test_true(memc_clone
);
3598 // The memcached_version needs to be done on a clone, because the server
3599 // will not toggle protocol on an connection.
3600 memcached_version(memc_clone
);
3602 if (libmemcached_util_version_check(memc_clone
, 1, 2, 0))
3604 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3605 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3606 test_true(rc
== MEMCACHED_SUCCESS
);
3607 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3611 return TEST_SKIPPED
;
3614 memcached_free(memc_clone
);
3616 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3619 static test_return_t
pre_murmur(memcached_st
*memc
)
3621 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3623 return TEST_SUCCESS
;
3626 static test_return_t
pre_jenkins(memcached_st
*memc
)
3628 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3630 return TEST_SUCCESS
;
3634 static test_return_t
pre_md5(memcached_st
*memc
)
3636 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3638 return TEST_SUCCESS
;
3641 static test_return_t
pre_crc(memcached_st
*memc
)
3643 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3645 return TEST_SUCCESS
;
3648 static test_return_t
pre_hsieh(memcached_st
*memc
)
3650 #ifdef HAVE_HSIEH_HASH
3651 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3652 return TEST_SUCCESS
;
3655 return TEST_SKIPPED
;
3659 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3661 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3663 return TEST_SUCCESS
;
3666 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3668 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3670 return TEST_SUCCESS
;
3673 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3675 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3677 return TEST_SUCCESS
;
3680 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3682 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3684 return TEST_SUCCESS
;
3687 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3689 memcached_return_t rc
;
3692 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3693 test_true(rc
== MEMCACHED_SUCCESS
);
3695 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3696 test_true(value
== 1);
3698 return TEST_SUCCESS
;
3701 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3703 memcached_return_t rc
;
3706 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3707 test_true(rc
== MEMCACHED_SUCCESS
);
3709 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3710 test_true(value
== 1);
3712 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3713 test_true(rc
== MEMCACHED_SUCCESS
);
3715 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3716 test_true(value
== MEMCACHED_HASH_MD5
);
3718 return TEST_SUCCESS
;
3722 @note This should be testing to see if the server really supports the binary protocol.
3724 static test_return_t
pre_binary(memcached_st
*memc
)
3726 memcached_return_t rc
= MEMCACHED_FAILURE
;
3727 memcached_st
*memc_clone
;
3729 memc_clone
= memcached_clone(NULL
, memc
);
3730 test_true(memc_clone
);
3731 // The memcached_version needs to be done on a clone, because the server
3732 // will not toggle protocol on an connection.
3733 memcached_version(memc_clone
);
3735 if (libmemcached_util_version_check(memc_clone
, 1, 2, 0))
3737 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3738 test_true(rc
== MEMCACHED_SUCCESS
);
3739 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3742 memcached_free(memc_clone
);
3744 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3747 static test_return_t
pre_sasl(memcached_st
*memc
)
3749 memcached_return_t rc
= MEMCACHED_FAILURE
;
3751 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3752 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3753 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3754 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3756 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3758 memcached_server_st
*servers
= memcached_servers_parse(server
);
3759 test_true(servers
!= NULL
);
3760 memcached_servers_reset(memc
);
3761 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3762 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3763 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3764 test_true(rc
== MEMCACHED_SUCCESS
);
3770 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3773 static test_return_t
pre_replication(memcached_st
*memc
)
3775 test_return_t test_rc
;
3776 test_rc
= pre_binary(memc
);
3778 if (test_rc
!= TEST_SUCCESS
)
3782 * Make sure that we store the item on all servers
3783 * (master + replicas == number of servers)
3785 memcached_return_t rc
;
3786 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3787 memcached_server_count(memc
) - 1);
3788 test_true(rc
== MEMCACHED_SUCCESS
);
3789 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3791 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3795 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3799 rc
= pre_replication(memc
);
3800 if (rc
!= TEST_SUCCESS
)
3803 rc
= pre_nonblock(memc
);
3809 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3812 #ifdef HARD_MALLOC_TESTS
3813 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3821 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3824 #ifdef HARD_MALLOC_TESTS
3825 void *ret
= malloc(size
+ 8);
3828 ret
= (void*)((caddr_t
)ret
+ 8);
3831 void *ret
= malloc(size
);
3836 memset(ret
, 0xff, size
);
3843 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3846 #ifdef HARD_MALLOC_TESTS
3847 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3848 void *nmem
= realloc(real_ptr
, size
+ 8);
3853 ret
= (void*)((caddr_t
)nmem
+ 8);
3858 return realloc(mem
, size
);
3863 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3866 #ifdef HARD_MALLOC_TESTS
3867 void *mem
= my_malloc(ptr
, nelem
* size
);
3870 memset(mem
, 0, nelem
* size
);
3875 return calloc(nelem
, size
);
3880 static test_return_t
set_prefix(memcached_st
*memc
)
3882 memcached_return_t rc
;
3883 const char *key
= "mine";
3886 /* Make sure be default none exists */
3887 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3888 test_true(rc
== MEMCACHED_FAILURE
);
3890 /* Test a clean set */
3891 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3892 test_true(rc
== MEMCACHED_SUCCESS
);
3894 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3895 test_true(memcmp(value
, key
, 4) == 0);
3896 test_true(rc
== MEMCACHED_SUCCESS
);
3898 /* Test that we can turn it off */
3899 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3900 test_true(rc
== MEMCACHED_SUCCESS
);
3902 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3903 test_true(rc
== MEMCACHED_FAILURE
);
3905 /* Now setup for main test */
3906 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3907 test_true(rc
== MEMCACHED_SUCCESS
);
3909 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3910 test_true(rc
== MEMCACHED_SUCCESS
);
3911 test_true(memcmp(value
, key
, 4) == 0);
3913 /* Set to Zero, and then Set to something too large */
3916 memset(long_key
, 0, 255);
3918 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3919 test_true(rc
== MEMCACHED_SUCCESS
);
3921 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3922 test_true(rc
== MEMCACHED_FAILURE
);
3923 test_true(value
== NULL
);
3925 /* Test a long key for failure */
3926 /* TODO, extend test to determine based on setting, what result should be */
3927 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3928 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3929 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3930 test_true(rc
== MEMCACHED_SUCCESS
);
3932 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3933 strcpy(long_key
, "This is more then the allotted number of characters");
3934 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3935 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3937 /* Test for a bad prefix, but with a short key */
3938 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3939 test_true(rc
== MEMCACHED_SUCCESS
);
3941 strcpy(long_key
, "dog cat");
3942 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3943 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3946 return TEST_SUCCESS
;
3950 #ifdef MEMCACHED_ENABLE_DEPRECATED
3951 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3953 void *test_ptr
= NULL
;
3956 memcached_malloc_fn malloc_cb
=
3957 (memcached_malloc_fn
)my_malloc
;
3958 cb_ptr
= *(void **)&malloc_cb
;
3959 memcached_return_t rc
;
3961 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3962 test_true(rc
== MEMCACHED_SUCCESS
);
3963 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3964 test_true(rc
== MEMCACHED_SUCCESS
);
3965 test_true(test_ptr
== cb_ptr
);
3969 memcached_realloc_fn realloc_cb
=
3970 (memcached_realloc_fn
)my_realloc
;
3971 cb_ptr
= *(void **)&realloc_cb
;
3972 memcached_return_t rc
;
3974 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3975 test_true(rc
== MEMCACHED_SUCCESS
);
3976 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3977 test_true(rc
== MEMCACHED_SUCCESS
);
3978 test_true(test_ptr
== cb_ptr
);
3982 memcached_free_fn free_cb
=
3983 (memcached_free_fn
)my_free
;
3984 cb_ptr
= *(void **)&free_cb
;
3985 memcached_return_t rc
;
3987 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3988 test_true(rc
== MEMCACHED_SUCCESS
);
3989 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3990 test_true(rc
== MEMCACHED_SUCCESS
);
3991 test_true(test_ptr
== cb_ptr
);
3994 return TEST_SUCCESS
;
3999 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4001 memcached_return_t rc
;
4002 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4003 my_realloc
, my_calloc
, NULL
);
4004 test_true(rc
== MEMCACHED_FAILURE
);
4006 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4007 my_realloc
, my_calloc
, NULL
);
4009 memcached_malloc_fn mem_malloc
;
4010 memcached_free_fn mem_free
;
4011 memcached_realloc_fn mem_realloc
;
4012 memcached_calloc_fn mem_calloc
;
4013 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4014 &mem_realloc
, &mem_calloc
);
4016 test_true(mem_malloc
== my_malloc
);
4017 test_true(mem_realloc
== my_realloc
);
4018 test_true(mem_calloc
== my_calloc
);
4019 test_true(mem_free
== my_free
);
4021 return TEST_SUCCESS
;
4024 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4027 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4028 memcached_hash_t hash
;
4029 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4030 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4033 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4034 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4036 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4038 if (hash
!= MEMCACHED_HASH_CRC
)
4039 return TEST_SKIPPED
;
4041 return TEST_SUCCESS
;
4044 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4047 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4048 memcached_hash_t hash
;
4049 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4050 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4053 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4054 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4056 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4058 if (hash
!= MEMCACHED_HASH_HSIEH
)
4059 return TEST_SKIPPED
;
4062 return TEST_SUCCESS
;
4065 static test_return_t
enable_cas(memcached_st
*memc
)
4067 unsigned int set
= 1;
4069 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4071 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4073 return TEST_SUCCESS
;
4076 return TEST_SKIPPED
;
4079 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4081 memcached_version(memc
);
4083 memcached_server_instance_st instance
=
4084 memcached_server_instance_by_position(memc
, 0);
4086 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4087 || instance
->minor_version
> 2)
4089 return TEST_SUCCESS
;
4092 return TEST_SKIPPED
;
4095 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4097 memcached_return_t rc
;
4100 memcached_servers_reset(memc
);
4102 if (stat("/tmp/memcached.socket", &buf
))
4103 return TEST_SKIPPED
;
4105 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4107 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4110 static test_return_t
pre_nodelay(memcached_st
*memc
)
4112 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4113 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4115 return TEST_SUCCESS
;
4118 static test_return_t
pre_settimer(memcached_st
*memc
)
4120 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4121 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4123 return TEST_SUCCESS
;
4126 static test_return_t
poll_timeout(memcached_st
*memc
)
4132 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4134 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4136 test_true(timeout
== 100);
4138 return TEST_SUCCESS
;
4141 static test_return_t
noreply_test(memcached_st
*memc
)
4143 memcached_return_t ret
;
4144 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4145 test_true(ret
== MEMCACHED_SUCCESS
);
4146 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4147 test_true(ret
== MEMCACHED_SUCCESS
);
4148 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4149 test_true(ret
== MEMCACHED_SUCCESS
);
4150 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4151 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4152 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4154 for (int count
=0; count
< 5; ++count
)
4156 for (size_t x
= 0; x
< 100; ++x
)
4159 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4163 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4166 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4169 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4172 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4175 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4181 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4185 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4186 ** API and is _ONLY_ done this way to verify that the library works the
4187 ** way it is supposed to do!!!!
4190 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4192 memcached_server_instance_st instance
=
4193 memcached_server_instance_by_position(memc
, x
);
4194 no_msg
+=(int)(instance
->cursor_active
);
4197 test_true(no_msg
== 0);
4198 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4201 ** Now validate that all items was set properly!
4203 for (size_t x
= 0; x
< 100; ++x
)
4207 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4210 char* value
=memcached_get(memc
, key
, strlen(key
),
4211 &length
, &flags
, &ret
);
4212 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4215 case 0: /* FALLTHROUGH */
4216 case 1: /* FALLTHROUGH */
4218 test_true(strncmp(value
, key
, len
) == 0);
4219 test_true(len
== length
);
4222 test_true(length
== len
* 2);
4225 test_true(length
== len
* 3);
4235 /* Try setting an illegal cas value (should not return an error to
4236 * the caller (because we don't expect a return message from the server)
4238 const char* keys
[]= {"0"};
4239 size_t lengths
[]= {1};
4242 memcached_result_st results_obj
;
4243 memcached_result_st
*results
;
4244 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4245 test_true(ret
== MEMCACHED_SUCCESS
);
4247 results
= memcached_result_create(memc
, &results_obj
);
4249 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4251 test_true(ret
== MEMCACHED_SUCCESS
);
4252 uint64_t cas
= memcached_result_cas(results
);
4253 memcached_result_free(&results_obj
);
4255 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4256 test_true(ret
== MEMCACHED_SUCCESS
);
4259 * The item will have a new cas value, so try to set it again with the old
4260 * value. This should fail!
4262 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4263 test_true(ret
== MEMCACHED_SUCCESS
);
4264 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4265 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4266 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4269 return TEST_SUCCESS
;
4272 static test_return_t
analyzer_test(memcached_st
*memc
)
4274 memcached_return_t rc
;
4275 memcached_stat_st
*memc_stat
;
4276 memcached_analysis_st
*report
;
4278 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4279 test_true(rc
== MEMCACHED_SUCCESS
);
4280 test_true(memc_stat
);
4282 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4283 test_true(rc
== MEMCACHED_SUCCESS
);
4287 memcached_stat_free(NULL
, memc_stat
);
4289 return TEST_SUCCESS
;
4292 /* Count the objects */
4293 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4294 const char *key
__attribute__((unused
)),
4295 size_t key_length
__attribute__((unused
)),
4298 size_t *counter
= (size_t *)context
;
4300 *counter
= *counter
+ 1;
4302 return MEMCACHED_SUCCESS
;
4305 static test_return_t
dump_test(memcached_st
*memc
)
4307 memcached_return_t rc
;
4309 memcached_dump_fn callbacks
[1];
4310 test_return_t main_rc
;
4312 callbacks
[0]= &callback_dump_counter
;
4314 /* No support for Binary protocol yet */
4315 if (memc
->flags
.binary_protocol
)
4316 return TEST_SUCCESS
;
4318 main_rc
= set_test3(memc
);
4320 test_true (main_rc
== TEST_SUCCESS
);
4322 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4323 test_true(rc
== MEMCACHED_SUCCESS
);
4325 /* We may have more then 32 if our previous flush has not completed */
4326 test_true(counter
>= 32);
4328 return TEST_SUCCESS
;
4331 #ifdef HAVE_LIBMEMCACHEDUTIL
4332 static void* connection_release(void *arg
)
4335 memcached_pool_st
* pool
;
4340 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4344 static test_return_t
connection_pool_test(memcached_st
*memc
)
4346 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4347 test_true(pool
!= NULL
);
4348 memcached_st
* mmc
[10];
4349 memcached_return_t rc
;
4351 for (size_t x
= 0; x
< 10; ++x
)
4353 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4354 test_true(mmc
[x
] != NULL
);
4355 test_true(rc
== MEMCACHED_SUCCESS
);
4358 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4359 test_true(rc
== MEMCACHED_SUCCESS
);
4363 memcached_pool_st
* pool
;
4365 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4366 pthread_create(&tid
, NULL
, connection_release
, &item
);
4367 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4368 test_true(rc
== MEMCACHED_SUCCESS
);
4369 pthread_join(tid
, NULL
);
4370 test_true(mmc
[9] == item
.mmc
);
4371 const char *key
= "key";
4372 size_t keylen
= strlen(key
);
4374 // verify that I can do ops with all connections
4375 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4376 test_true(rc
== MEMCACHED_SUCCESS
);
4378 for (size_t x
= 0; x
< 10; ++x
)
4380 uint64_t number_value
;
4381 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4382 test_true(rc
== MEMCACHED_SUCCESS
);
4383 test_true(number_value
== (x
+1));
4387 for (size_t x
= 0; x
< 10; ++x
)
4389 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4393 /* verify that I can set behaviors on the pool when I don't have all
4394 * of the connections in the pool. It should however be enabled
4395 * when I push the item into the pool
4397 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4398 test_true(mmc
[0] != NULL
);
4400 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4401 test_true(rc
== MEMCACHED_SUCCESS
);
4403 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4404 test_true(mmc
[1] != NULL
);
4406 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4407 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4408 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4410 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4411 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4412 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4415 test_true(memcached_pool_destroy(pool
) == memc
);
4416 return TEST_SUCCESS
;
4419 static test_return_t
util_version_test(memcached_st
*memc
)
4423 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4424 test_true(if_successful
== true);
4426 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4427 test_true(if_successful
== false);
4429 memcached_server_instance_st instance
=
4430 memcached_server_instance_by_position(memc
, 0);
4432 memcached_version(memc
);
4434 // We only use one binary when we test, so this should be just fine.
4435 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->micro_version
, instance
->minor_version
);
4436 test_true(if_successful
== true);
4438 if (instance
->minor_version
> 0)
4439 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->micro_version
, instance
->minor_version
-1);
4440 else if (instance
->micro_version
> 0)
4441 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->micro_version
- 1, instance
->minor_version
);
4442 else if (instance
->major_version
> 0)
4443 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
-1, instance
->micro_version
, instance
->minor_version
);
4445 test_true(if_successful
== true);
4447 if (instance
->minor_version
> 0)
4448 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->micro_version
, instance
->minor_version
+1);
4449 else if (instance
->micro_version
> 0)
4450 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->micro_version
+1, instance
->minor_version
);
4451 else if (instance
->major_version
> 0)
4452 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
+1, instance
->micro_version
, instance
->minor_version
);
4454 test_true(if_successful
== false);
4456 return TEST_SUCCESS
;
4459 static test_return_t
ping_test(memcached_st
*memc
)
4461 memcached_return_t rc
;
4462 memcached_server_instance_st instance
=
4463 memcached_server_instance_by_position(memc
, 0);
4465 // Test both the version that returns a code, and the one that does not.
4466 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4467 memcached_server_port(instance
), NULL
));
4469 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4470 memcached_server_port(instance
), &rc
));
4472 test_true(rc
== MEMCACHED_SUCCESS
);
4474 return TEST_SUCCESS
;
4478 static test_return_t
replication_set_test(memcached_st
*memc
)
4480 memcached_return_t rc
;
4481 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4482 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4484 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4485 test_true(rc
== MEMCACHED_SUCCESS
);
4488 ** We are using the quiet commands to store the replicas, so we need
4489 ** to ensure that all of them are processed before we can continue.
4490 ** In the test we go directly from storing the object to trying to
4491 ** receive the object from all of the different servers, so we
4492 ** could end up in a race condition (the memcached server hasn't yet
4493 ** processed the quiet command from the replication set when it process
4494 ** the request from the other client (created by the clone)). As a
4495 ** workaround for that we call memcached_quit to send the quit command
4496 ** to the server and wait for the response ;-) If you use the test code
4497 ** as an example for your own code, please note that you shouldn't need
4500 memcached_quit(memc
);
4503 ** "bubba" should now be stored on all of our servers. We don't have an
4504 ** easy to use API to address each individual server, so I'll just iterate
4505 ** through a bunch of "master keys" and I should most likely hit all of the
4508 for (int x
= 'a'; x
<= 'z'; ++x
)
4510 char key
[2]= { [0]= (char)x
};
4513 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4515 test_true(rc
== MEMCACHED_SUCCESS
);
4516 test_true(val
!= NULL
);
4520 memcached_free(memc_clone
);
4522 return TEST_SUCCESS
;
4525 static test_return_t
replication_get_test(memcached_st
*memc
)
4527 memcached_return_t rc
;
4530 * Don't do the following in your code. I am abusing the internal details
4531 * within the library, and this is not a supported interface.
4532 * This is to verify correct behavior in the library
4534 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4536 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4537 memcached_server_instance_st instance
=
4538 memcached_server_instance_by_position(memc_clone
, host
);
4540 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4542 for (int x
= 'a'; x
<= 'z'; ++x
)
4544 char key
[2]= { [0]= (char)x
};
4547 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4549 test_true(rc
== MEMCACHED_SUCCESS
);
4550 test_true(val
!= NULL
);
4554 memcached_free(memc_clone
);
4557 return TEST_SUCCESS
;
4560 static test_return_t
replication_mget_test(memcached_st
*memc
)
4562 memcached_return_t rc
;
4563 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4564 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4566 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4567 size_t len
[]= { 5, 4, 4, 4 };
4569 for (size_t x
= 0; x
< 4; ++x
)
4571 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4572 test_true(rc
== MEMCACHED_SUCCESS
);
4576 ** We are using the quiet commands to store the replicas, so we need
4577 ** to ensure that all of them are processed before we can continue.
4578 ** In the test we go directly from storing the object to trying to
4579 ** receive the object from all of the different servers, so we
4580 ** could end up in a race condition (the memcached server hasn't yet
4581 ** processed the quiet command from the replication set when it process
4582 ** the request from the other client (created by the clone)). As a
4583 ** workaround for that we call memcached_quit to send the quit command
4584 ** to the server and wait for the response ;-) If you use the test code
4585 ** as an example for your own code, please note that you shouldn't need
4588 memcached_quit(memc
);
4591 * Don't do the following in your code. I am abusing the internal details
4592 * within the library, and this is not a supported interface.
4593 * This is to verify correct behavior in the library
4595 memcached_result_st result_obj
;
4596 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4598 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4599 memcached_server_instance_st instance
=
4600 memcached_server_instance_by_position(new_clone
, host
);
4601 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4603 for (int x
= 'a'; x
<= 'z'; ++x
)
4605 char key
[2]= { [0]= (char)x
, [1]= 0 };
4607 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4608 test_true(rc
== MEMCACHED_SUCCESS
);
4610 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4614 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4618 test_true(hits
== 4);
4619 memcached_result_free(&result_obj
);
4622 memcached_free(new_clone
);
4625 memcached_free(memc_clone
);
4627 return TEST_SUCCESS
;
4630 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4632 memcached_result_st result_obj
;
4633 memcached_return_t rc
;
4634 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4635 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4636 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4638 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4639 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4641 for (size_t x
= 0; x
< 7; ++x
)
4643 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4644 test_true(rc
== MEMCACHED_SUCCESS
);
4647 memcached_quit(memc
);
4649 for (size_t x
= 0; x
< 7; ++x
)
4651 const char key
[2]= { [0]= (const char)x
};
4653 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4654 test_true(rc
== MEMCACHED_SUCCESS
);
4656 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4660 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4664 test_true(hits
== 7);
4665 memcached_result_free(&result_obj
);
4667 memcached_free(memc_clone
);
4668 return TEST_SUCCESS
;
4671 static test_return_t
replication_delete_test(memcached_st
*memc
)
4673 memcached_return_t rc
;
4674 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4675 /* Delete the items from all of the servers except 1 */
4676 uint64_t repl
= memcached_behavior_get(memc
,
4677 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4678 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4680 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4681 size_t len
[]= { 5, 4, 4, 4 };
4683 for (size_t x
= 0; x
< 4; ++x
)
4685 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4686 test_true(rc
== MEMCACHED_SUCCESS
);
4690 * Don't do the following in your code. I am abusing the internal details
4691 * within the library, and this is not a supported interface.
4692 * This is to verify correct behavior in the library
4694 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4695 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4697 memcached_server_instance_st instance
=
4698 memcached_server_instance_by_position(memc_clone
, x
);
4700 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4701 if (++hash
== memc_clone
->number_of_hosts
)
4705 memcached_result_st result_obj
;
4706 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4708 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4710 const char key
[2]= { [0]= (const char)x
};
4712 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4713 test_true(rc
== MEMCACHED_SUCCESS
);
4715 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4719 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4723 test_true(hits
== 4);
4724 memcached_result_free(&result_obj
);
4727 memcached_free(memc_clone
);
4729 return TEST_SUCCESS
;
4733 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4737 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4738 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4739 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4740 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4741 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4742 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4743 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4744 #ifdef HAVE_HSIEH_HASH
4745 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4747 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4748 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4749 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4751 return TEST_SUCCESS
;
4755 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4757 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4758 #ifdef HAVE_HSIEH_HASH
4759 expected_rc
= MEMCACHED_SUCCESS
;
4761 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4762 (uint64_t)MEMCACHED_HASH_HSIEH
);
4763 test_true(rc
== expected_rc
);
4765 return TEST_SUCCESS
;
4768 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4773 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4777 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4778 test_true(one_at_a_time_values
[x
] == hash_val
);
4781 return TEST_SUCCESS
;
4784 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4789 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4793 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4794 test_true(md5_values
[x
] == hash_val
);
4797 return TEST_SUCCESS
;
4800 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4805 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4809 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4810 test_true(crc_values
[x
] == hash_val
);
4813 return TEST_SUCCESS
;
4816 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4821 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4825 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4826 test_true(fnv1_64_values
[x
] == hash_val
);
4829 return TEST_SUCCESS
;
4832 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4837 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4841 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4842 test_true(fnv1a_64_values
[x
] == hash_val
);
4845 return TEST_SUCCESS
;
4848 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4854 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4858 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4859 test_true(fnv1_32_values
[x
] == hash_val
);
4862 return TEST_SUCCESS
;
4865 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4870 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4874 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4875 test_true(fnv1a_32_values
[x
] == hash_val
);
4878 return TEST_SUCCESS
;
4881 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4886 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4890 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4891 test_true(hsieh_values
[x
] == hash_val
);
4894 return TEST_SUCCESS
;
4897 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4899 #ifdef WORDS_BIGENDIAN
4900 return TEST_SKIPPED
;
4905 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4909 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4910 test_true(murmur_values
[x
] == hash_val
);
4913 return TEST_SUCCESS
;
4917 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4923 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4927 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4928 test_true(jenkins_values
[x
] == hash_val
);
4931 return TEST_SUCCESS
;
4934 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4937 return libhashkit_md5(string
, string_length
);
4940 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4943 return libhashkit_crc32(string
, string_length
);
4946 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4950 const hashkit_st
*kit
;
4952 hashkit_return_t hash_rc
;
4954 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};
4955 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};
4957 kit
= memcached_get_hashkit(memc
);
4959 hashkit_clone(&new_kit
, kit
);
4960 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4961 test_true(hash_rc
== HASHKIT_SUCCESS
);
4963 memcached_set_hashkit(memc
, &new_kit
);
4966 Verify Setting the hash.
4968 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4972 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4973 test_true(md5_values
[x
] == hash_val
);
4978 Now check memcached_st.
4980 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4984 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4985 test_true(md5_hosts
[x
] == hash_val
);
4988 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4989 test_true(hash_rc
== HASHKIT_SUCCESS
);
4991 memcached_set_hashkit(memc
, &new_kit
);
4994 Verify Setting the hash.
4996 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5000 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
5001 test_true(crc_values
[x
] == hash_val
);
5004 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5008 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5009 test_true(crc_hosts
[x
] == hash_val
);
5012 return TEST_SUCCESS
;
5016 Test case adapted from John Gorman <johngorman2@gmail.com>
5018 We are testing the error condition when we connect to a server via memcached_get()
5019 but find that the server is not available.
5021 static test_return_t
memcached_get_MEMCACHED_SOME_ERRORS(memcached_st
*memc
)
5024 memcached_st
*tl_memc_h
;
5025 memcached_server_st
*servers
;
5027 const char *key
= "MemcachedLives";
5030 memcached_return rc
;
5034 tl_memc_h
= memcached_create(NULL
);
5035 servers
= memcached_servers_parse("localhost:9898"); // This server should not exist
5036 memcached_server_push(tl_memc_h
, servers
);
5037 memcached_server_list_free(servers
);
5039 // See if memcached is reachable.
5040 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5045 test_true(value
); // Pointer won't be zero so this is fine.
5048 test_true(len
== 0);
5049 test_true(rc
== MEMCACHED_SOME_ERRORS
);
5051 return TEST_SUCCESS
;
5055 We connect to a server which exists, but search for a key that does not exist.
5057 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5059 const char *key
= "MemcachedKeyNotEXIST";
5062 memcached_return rc
;
5065 // See if memcached is reachable.
5066 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5071 test_true(value
); // Pointer won't be zero so this is fine.
5074 test_true(len
== 0);
5075 test_true(rc
== MEMCACHED_NOTFOUND
);
5077 return TEST_SUCCESS
;
5081 Test case adapted from John Gorman <johngorman2@gmail.com>
5083 We are testing the error condition when we connect to a server via memcached_get_by_key()
5084 but find that the server is not available.
5086 static test_return_t
memcached_get_by_key_MEMCACHED_SOME_ERRORS(memcached_st
*memc
)
5089 memcached_st
*tl_memc_h
;
5090 memcached_server_st
*servers
;
5092 const char *key
= "MemcachedLives";
5095 memcached_return rc
;
5099 tl_memc_h
= memcached_create(NULL
);
5100 servers
= memcached_servers_parse("localhost:9898"); // This server should not exist
5101 memcached_server_push(tl_memc_h
, servers
);
5102 memcached_server_list_free(servers
);
5104 // See if memcached is reachable.
5105 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5110 test_true(value
); // Pointer won't be zero so this is fine.
5113 test_true(len
== 0);
5114 test_true(rc
== MEMCACHED_SOME_ERRORS
);
5116 return TEST_SUCCESS
;
5120 We connect to a server which exists, but search for a key that does not exist.
5122 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5124 const char *key
= "MemcachedKeyNotEXIST";
5127 memcached_return rc
;
5130 // See if memcached is reachable.
5131 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5136 test_true(value
); // Pointer won't be zero so this is fine.
5139 test_true(len
== 0);
5140 test_true(rc
== MEMCACHED_NOTFOUND
);
5142 return TEST_SUCCESS
;
5146 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5148 memcached_return_t rc
;
5151 memcached_server_st
*server_pool
;
5156 memc
= memcached_create(NULL
);
5159 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5160 test_true(rc
== MEMCACHED_SUCCESS
);
5162 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5163 test_true(value
== 1);
5165 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5166 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5169 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");
5170 memcached_server_push(memc
, server_pool
);
5172 /* verify that the server list was parsed okay. */
5173 test_true(memcached_server_count(memc
) == 8);
5174 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5175 test_true(server_pool
[0].port
== 11211);
5176 test_true(server_pool
[0].weight
== 600);
5177 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5178 test_true(server_pool
[2].port
== 11211);
5179 test_true(server_pool
[2].weight
== 200);
5180 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5181 test_true(server_pool
[7].port
== 11211);
5182 test_true(server_pool
[7].weight
== 100);
5184 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5185 * us test the boundary wraparound.
5187 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5189 /* verify the standard ketama set. */
5190 for (x
= 0; x
< 99; x
++)
5192 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5193 memcached_server_instance_st instance
=
5194 memcached_server_instance_by_position(memc
, server_idx
);
5195 const char *hostname
= memcached_server_name(instance
);
5197 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5200 memcached_server_list_free(server_pool
);
5201 memcached_free(memc
);
5203 return TEST_SUCCESS
;
5206 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5208 memcached_return_t rc
;
5211 memcached_server_st
*server_pool
;
5216 memc
= memcached_create(NULL
);
5219 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5220 test_true(rc
== MEMCACHED_SUCCESS
);
5222 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5223 test_true(value
== 1);
5225 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5226 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5228 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");
5229 memcached_server_push(memc
, server_pool
);
5231 /* verify that the server list was parsed okay. */
5232 test_true(memcached_server_count(memc
) == 8);
5233 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5234 test_true(server_pool
[0].port
== 11211);
5235 test_true(server_pool
[0].weight
== 600);
5236 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5237 test_true(server_pool
[2].port
== 11211);
5238 test_true(server_pool
[2].weight
== 200);
5239 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5240 test_true(server_pool
[7].port
== 11211);
5241 test_true(server_pool
[7].weight
== 100);
5243 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5244 * us test the boundary wraparound.
5246 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5248 /* verify the standard ketama set. */
5249 for (x
= 0; x
< 99; x
++)
5251 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5253 memcached_server_instance_st instance
=
5254 memcached_server_instance_by_position(memc
, server_idx
);
5256 const char *hostname
= memcached_server_name(instance
);
5258 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5261 memcached_server_list_free(server_pool
);
5262 memcached_free(memc
);
5264 return TEST_SUCCESS
;
5267 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5269 test_return_t test_rc
;
5270 test_rc
= pre_binary(memc
);
5272 if (test_rc
!= TEST_SUCCESS
)
5275 memcached_return_t ret
;
5276 const char *key
= "regression_bug_434484";
5277 size_t keylen
= strlen(key
);
5279 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5280 test_true(ret
== MEMCACHED_NOTSTORED
);
5282 size_t size
= 2048 * 1024;
5283 void *data
= calloc(1, size
);
5284 test_true(data
!= NULL
);
5285 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5286 test_true(ret
== MEMCACHED_E2BIG
);
5289 return TEST_SUCCESS
;
5292 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5294 test_return_t test_rc
;
5295 test_rc
= pre_binary(memc
);
5297 if (test_rc
!= TEST_SUCCESS
)
5300 memcached_return_t rc
;
5302 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5305 * I only want to hit only _one_ server so I know the number of requests I'm
5306 * sending in the pipleine to the server. Let's try to do a multiget of
5307 * 1024 (that should satisfy most users don't you think?). Future versions
5308 * will include a mget_execute function call if you need a higher number.
5310 uint32_t number_of_hosts
= memcached_server_count(memc
);
5311 memc
->number_of_hosts
= 1;
5312 const size_t max_keys
= 1024;
5313 char **keys
= calloc(max_keys
, sizeof(char*));
5314 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5316 for (size_t x
= 0; x
< max_keys
; ++x
)
5320 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5322 test_true(keys
[x
] != NULL
);
5326 * Run two times.. the first time we should have 100% cache miss,
5327 * and the second time we should have 100% cache hits
5329 for (size_t y
= 0; y
< 2; y
++)
5331 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5332 test_true(rc
== MEMCACHED_SUCCESS
);
5333 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5337 /* The first iteration should give me a 100% cache miss. verify that*/
5338 char blob
[1024]= { 0 };
5340 test_true(counter
== 0);
5342 for (size_t x
= 0; x
< max_keys
; ++x
)
5344 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5345 blob
, sizeof(blob
), 0, 0);
5346 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5351 /* Verify that we received all of the key/value pairs */
5352 test_true(counter
== max_keys
);
5356 /* Release allocated resources */
5357 for (size_t x
= 0; x
< max_keys
; ++x
)
5364 memc
->number_of_hosts
= number_of_hosts
;
5366 return TEST_SUCCESS
;
5369 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5371 memcached_return_t rc
;
5372 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5373 test_true(rc
== MEMCACHED_SUCCESS
);
5375 return regression_bug_434843(memc
);
5378 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5380 memcached_return_t rc
;
5381 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5382 test_true(rc
== MEMCACHED_SUCCESS
);
5384 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5385 test_true(rc
== MEMCACHED_SUCCESS
);
5386 test_true(bytes
!= NULL
);
5387 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5389 test_true(rc
== MEMCACHED_SUCCESS
);
5390 test_true(bytes_read
!= NULL
);
5392 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5393 "bytes_written", &rc
);
5394 test_true(rc
== MEMCACHED_SUCCESS
);
5395 test_true(bytes_written
!= NULL
);
5397 test_true(strcmp(bytes
, bytes_read
) != 0);
5398 test_true(strcmp(bytes
, bytes_written
) != 0);
5400 /* Release allocated resources */
5403 free(bytes_written
);
5404 memcached_stat_free(NULL
, memc_stat
);
5406 return TEST_SUCCESS
;
5410 * The test case isn't obvious so I should probably document why
5411 * it works the way it does. Bug 442914 was caused by a bug
5412 * in the logic in memcached_purge (it did not handle the case
5413 * where the number of bytes sent was equal to the watermark).
5414 * In this test case, create messages so that we hit that case
5415 * and then disable noreply mode and issue a new command to
5416 * verify that it isn't stuck. If we change the format for the
5417 * delete command or the watermarks, we need to update this
5420 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5422 memcached_return_t rc
;
5423 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5424 test_true(rc
== MEMCACHED_SUCCESS
);
5425 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5427 uint32_t number_of_hosts
= memcached_server_count(memc
);
5428 memc
->number_of_hosts
= 1;
5433 for (uint32_t x
= 0; x
< 250; ++x
)
5435 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5436 rc
= memcached_delete(memc
, k
, len
, 0);
5437 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5440 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5443 rc
= memcached_delete(memc
, k
, len
, 0);
5444 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5446 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5447 test_true(rc
== MEMCACHED_SUCCESS
);
5448 rc
= memcached_delete(memc
, k
, len
, 0);
5449 test_true(rc
== MEMCACHED_NOTFOUND
);
5451 memc
->number_of_hosts
= number_of_hosts
;
5453 return TEST_SUCCESS
;
5456 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5458 memcached_server_instance_st instance_one
;
5459 memcached_server_instance_st instance_two
;
5461 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5462 return TEST_SKIPPED
;
5464 memcached_return_t rc
;
5466 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5467 test_true(rc
== MEMCACHED_SUCCESS
);
5469 const size_t max_keys
= 100;
5470 char **keys
= calloc(max_keys
, sizeof(char*));
5471 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5473 for (size_t x
= 0; x
< max_keys
; ++x
)
5477 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5479 test_true(keys
[x
] != NULL
);
5480 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5481 test_true(rc
== MEMCACHED_SUCCESS
);
5485 ** We are using the quiet commands to store the replicas, so we need
5486 ** to ensure that all of them are processed before we can continue.
5487 ** In the test we go directly from storing the object to trying to
5488 ** receive the object from all of the different servers, so we
5489 ** could end up in a race condition (the memcached server hasn't yet
5490 ** processed the quiet command from the replication set when it process
5491 ** the request from the other client (created by the clone)). As a
5492 ** workaround for that we call memcached_quit to send the quit command
5493 ** to the server and wait for the response ;-) If you use the test code
5494 ** as an example for your own code, please note that you shouldn't need
5497 memcached_quit(memc
);
5499 /* Verify that all messages are stored, and we didn't stuff too much
5502 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5503 test_true(rc
== MEMCACHED_SUCCESS
);
5506 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5507 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5508 /* Verify that we received all of the key/value pairs */
5509 test_true(counter
== max_keys
);
5511 memcached_quit(memc
);
5513 * Don't do the following in your code. I am abusing the internal details
5514 * within the library, and this is not a supported interface.
5515 * This is to verify correct behavior in the library. Fake that two servers
5518 instance_one
= memcached_server_instance_by_position(memc
, 0);
5519 instance_two
= memcached_server_instance_by_position(memc
, 2);
5520 in_port_t port0
= instance_one
->port
;
5521 in_port_t port2
= instance_two
->port
;
5523 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5524 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5526 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5527 test_true(rc
== MEMCACHED_SUCCESS
);
5530 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5531 test_true(counter
== (unsigned int)max_keys
);
5533 /* restore the memc handle */
5534 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5535 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5537 memcached_quit(memc
);
5539 /* Remove half of the objects */
5540 for (size_t x
= 0; x
< max_keys
; ++x
)
5544 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5545 test_true(rc
== MEMCACHED_SUCCESS
);
5549 memcached_quit(memc
);
5550 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5551 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5553 /* now retry the command, this time we should have cache misses */
5554 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5555 test_true(rc
== MEMCACHED_SUCCESS
);
5558 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5559 test_true(counter
== (unsigned int)(max_keys
>> 1));
5561 /* Release allocated resources */
5562 for (size_t x
= 0; x
< max_keys
; ++x
)
5569 /* restore the memc handle */
5570 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5571 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5573 return TEST_SUCCESS
;
5576 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5578 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5579 test_true(memc_clone
!= NULL
);
5580 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5583 if (libmemcached_util_version_check(memc_clone
, 1, 1, 2))
5585 /* Binary protocol doesn't support deferred delete */
5586 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5587 test_true(bin_clone
!= NULL
);
5588 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5589 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5590 memcached_free(bin_clone
);
5592 memcached_quit(memc_clone
);
5594 /* If we know the server version, deferred delete should fail
5595 * with invalid arguments */
5596 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5598 /* If we don't know the server version, we should get a protocol error */
5599 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5601 /* but there is a bug in some of the memcached servers (1.4) that treats
5602 * the counter as noreply so it doesn't send the proper error message
5604 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5606 /* And buffered mode should be disabled and we should get protocol error */
5607 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5608 rc
= memcached_delete(memc
, "foo", 3, 1);
5609 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5611 /* Same goes for noreply... */
5612 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5613 rc
= memcached_delete(memc
, "foo", 3, 1);
5614 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5616 /* but a normal request should go through (and be buffered) */
5617 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5618 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5620 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5621 /* unbuffered noreply should be success */
5622 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5623 /* unbuffered with reply should be not found... */
5624 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5625 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5628 memcached_free(memc_clone
);
5629 return TEST_SUCCESS
;
5633 /* Test memcached_server_get_last_disconnect
5634 * For a working server set, shall be NULL
5635 * For a set of non existing server, shall not be NULL
5637 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5639 memcached_return_t rc
;
5640 memcached_server_instance_st disconnected_server
;
5642 /* With the working set of server */
5643 const char *key
= "marmotte";
5644 const char *value
= "milka";
5646 memcached_reset_last_disconnected_server(memc
);
5647 rc
= memcached_set(memc
, key
, strlen(key
),
5648 value
, strlen(value
),
5649 (time_t)0, (uint32_t)0);
5650 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5652 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5653 test_true(disconnected_server
== NULL
);
5655 /* With a non existing server */
5657 memcached_server_st
*servers
;
5659 const char *server_list
= "localhost:9";
5661 servers
= memcached_servers_parse(server_list
);
5663 mine
= memcached_create(NULL
);
5664 rc
= memcached_server_push(mine
, servers
);
5665 test_true(rc
== MEMCACHED_SUCCESS
);
5666 memcached_server_list_free(servers
);
5669 rc
= memcached_set(mine
, key
, strlen(key
),
5670 value
, strlen(value
),
5671 (time_t)0, (uint32_t)0);
5672 test_true(rc
!= MEMCACHED_SUCCESS
);
5674 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5675 if (disconnected_server
== NULL
)
5677 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5680 test_true(disconnected_server
!= NULL
);
5681 test_true(memcached_server_port(disconnected_server
)== 9);
5682 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5684 memcached_quit(mine
);
5685 memcached_free(mine
);
5687 return TEST_SUCCESS
;
5690 static test_return_t
test_verbosity(memcached_st
*memc
)
5692 memcached_verbosity(memc
, 3);
5694 return TEST_SUCCESS
;
5697 static test_return_t
test_server_failure(memcached_st
*memc
)
5699 memcached_st
*local_memc
;
5700 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5702 local_memc
= memcached_create(NULL
);
5704 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5705 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5707 uint32_t server_count
= memcached_server_count(local_memc
);
5709 test_true(server_count
== 1);
5711 // Disable the server
5712 instance
= memcached_server_instance_by_position(local_memc
, 0);
5713 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5715 memcached_return_t rc
;
5716 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5718 (time_t)0, (uint32_t)0);
5719 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5721 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5722 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5724 (time_t)0, (uint32_t)0);
5725 test_true(rc
== MEMCACHED_SUCCESS
);
5728 memcached_free(local_memc
);
5730 return TEST_SUCCESS
;
5733 static test_return_t
test_cull_servers(memcached_st
*memc
)
5735 uint32_t count
= memcached_server_count(memc
);
5737 // Do not do this in your code, it is not supported.
5738 memc
->servers
[1].state
.is_dead
= true;
5739 memc
->state
.is_time_for_rebuild
= true;
5741 uint32_t new_count
= memcached_server_count(memc
);
5742 test_true(count
== new_count
);
5745 test_true(count
== new_count
+ 1 );
5748 return TEST_SUCCESS
;
5752 * This test ensures that the failure counter isn't incremented during
5753 * normal termination of the memcached instance.
5755 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5757 memcached_return_t rc
;
5758 memcached_server_instance_st instance
;
5760 /* Set value to force connection to the server */
5761 const char *key
= "marmotte";
5762 const char *value
= "milka";
5765 * Please note that I'm abusing the internal structures in libmemcached
5766 * in a non-portable way and you shouldn't be doing this. I'm only
5767 * doing this in order to verify that the library works the way it should
5769 uint32_t number_of_hosts
= memcached_server_count(memc
);
5770 memc
->number_of_hosts
= 1;
5772 /* Ensure that we are connected to the server by setting a value */
5773 rc
= memcached_set(memc
, key
, strlen(key
),
5774 value
, strlen(value
),
5775 (time_t)0, (uint32_t)0);
5776 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5779 instance
= memcached_server_instance_by_position(memc
, 0);
5780 /* The test is to see that the memcached_quit doesn't increase the
5781 * the server failure conter, so let's ensure that it is zero
5782 * before sending quit
5784 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5786 memcached_quit(memc
);
5788 /* Verify that it memcached_quit didn't increment the failure counter
5789 * Please note that this isn't bullet proof, because an error could
5792 test_true(instance
->server_failure_counter
== 0);
5794 /* restore the instance */
5795 memc
->number_of_hosts
= number_of_hosts
;
5797 return TEST_SUCCESS
;
5804 * Test that ensures mget_execute does not end into recursive calls that finally fails
5806 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5808 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5809 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5810 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5811 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5812 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5815 return TEST_SKIPPED
; // My MAC can't handle this test
5819 * I only want to hit _one_ server so I know the number of requests I'm
5820 * sending in the pipeline.
5822 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5823 memc
->number_of_hosts
= 1;
5824 size_t max_keys
= 20480;
5827 char **keys
= calloc(max_keys
, sizeof(char*));
5828 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5830 /* First add all of the items.. */
5832 char blob
[1024]= { 0 };
5833 memcached_return rc
;
5834 for (size_t x
= 0; x
< max_keys
; ++x
)
5837 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5839 assert(keys
[x
] != NULL
);
5840 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5842 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5844 break; // We are out of business
5847 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5849 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5852 sleep(1);// We will try to sleep
5855 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5857 // We failed to send everything.
5862 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5865 /* Try to get all of them with a large multiget */
5867 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5868 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5869 (size_t)max_keys
, callbacks
, &counter
, 1);
5871 assert(rc
== MEMCACHED_SUCCESS
);
5872 char* the_value
= NULL
;
5873 char the_key
[MEMCACHED_MAX_KEY
];
5874 size_t the_key_length
;
5875 size_t the_value_length
;
5879 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5881 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5887 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5890 assert(rc
== MEMCACHED_END
);
5892 /* Verify that we got all of the items */
5893 assert(counter
== max_keys
);
5896 /* Release all allocated resources */
5897 for (size_t x
= 0; x
< max_keys
; ++x
)
5904 memc
->number_of_hosts
= number_of_hosts
;
5906 return TEST_SUCCESS
;
5909 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5913 memcached_st
*memc
= memcached_create(NULL
);
5915 memcached_server_add(memc
, "10.2.3.4", 11211);
5917 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5918 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5919 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5920 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5921 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5922 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5924 memcached_return_t rc
;
5928 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5930 test_true(rc
== MEMCACHED_TIMEOUT
);
5932 memcached_free(memc
);
5934 return TEST_SUCCESS
;
5937 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5939 fprintf(stderr
, "Iteration #%u: ", it
);
5941 if(error
== MEMCACHED_ERRNO
)
5943 fprintf(stderr
, "system error %d from %s: %s\n",
5944 errno
, what
, strerror(errno
));
5948 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5949 memcached_strerror(mc
, error
));
5953 #define TEST_CONSTANT_CREATION 200
5955 static test_return_t
regression_bug_(memcached_st
*memc
)
5957 const char *remote_server
;
5960 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5962 return TEST_SKIPPED
;
5965 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5967 memcached_st
* mc
= memcached_create(NULL
);
5968 memcached_return rc
;
5970 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5971 if (rc
!= MEMCACHED_SUCCESS
)
5973 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5976 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5977 if (rc
!= MEMCACHED_SUCCESS
)
5979 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5982 rc
= memcached_server_add(mc
, remote_server
, 0);
5983 if (rc
!= MEMCACHED_SUCCESS
)
5985 memcached_die(mc
, rc
, "memcached_server_add", x
);
5988 const char *set_key
= "akey";
5989 const size_t set_key_len
= strlen(set_key
);
5990 const char *set_value
= "a value";
5991 const size_t set_value_len
= strlen(set_value
);
5993 if (rc
== MEMCACHED_SUCCESS
)
5997 size_t get_value_len
;
5999 uint32_t get_value_flags
;
6001 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
6002 &get_value_flags
, &rc
);
6003 if (rc
!= MEMCACHED_SUCCESS
)
6005 memcached_die(mc
, rc
, "memcached_get", x
);
6011 (get_value_len
!= set_value_len
6012 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
6014 fprintf(stderr
, "Values don't match?\n");
6015 rc
= MEMCACHED_FAILURE
;
6021 rc
= memcached_set(mc
,
6022 set_key
, set_key_len
,
6023 set_value
, set_value_len
,
6027 if (rc
!= MEMCACHED_SUCCESS
)
6029 memcached_die(mc
, rc
, "memcached_set", x
);
6036 if (rc
!= MEMCACHED_SUCCESS
)
6042 return TEST_SUCCESS
;
6046 * Test that the sasl authentication works. We cannot use the default
6047 * pool of servers, because that would require that all servers we want
6048 * to test supports SASL authentication, and that they use the default
6051 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6053 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6054 memcached_return_t rc
;
6056 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6057 test_true(rc
== MEMCACHED_SUCCESS
);
6058 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6059 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6060 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6061 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6062 memcached_quit(memc
);
6064 rc
= memcached_set_sasl_auth_data(memc
,
6065 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6066 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6067 test_true(rc
== MEMCACHED_SUCCESS
);
6069 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6070 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6071 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6073 memcached_quit(memc
);
6074 return TEST_SUCCESS
;
6077 return TEST_FAILURE
;
6081 /* Clean the server before beginning testing */
6083 {"flush", 0, (test_callback_fn
)flush_test
},
6084 {"init", 0, (test_callback_fn
)init_test
},
6085 {"allocation", 0, (test_callback_fn
)allocation_test
},
6086 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6087 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6088 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6089 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6090 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6091 {"clone_test", 0, (test_callback_fn
)clone_test
},
6092 {"connection_test", 0, (test_callback_fn
)connection_test
},
6093 {"callback_test", 0, (test_callback_fn
)callback_test
},
6094 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6095 {"error", 0, (test_callback_fn
)error_test
},
6096 {"set", 0, (test_callback_fn
)set_test
},
6097 {"set2", 0, (test_callback_fn
)set_test2
},
6098 {"set3", 0, (test_callback_fn
)set_test3
},
6099 {"dump", 1, (test_callback_fn
)dump_test
},
6100 {"add", 1, (test_callback_fn
)add_test
},
6101 {"replace", 1, (test_callback_fn
)replace_test
},
6102 {"delete", 1, (test_callback_fn
)delete_test
},
6103 {"get", 1, (test_callback_fn
)get_test
},
6104 {"get2", 0, (test_callback_fn
)get_test2
},
6105 {"get3", 0, (test_callback_fn
)get_test3
},
6106 {"get4", 0, (test_callback_fn
)get_test4
},
6107 {"partial mget", 0, (test_callback_fn
)get_test5
},
6108 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6109 {"increment", 0, (test_callback_fn
)increment_test
},
6110 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6111 {"decrement", 0, (test_callback_fn
)decrement_test
},
6112 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6113 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6114 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6115 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6116 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6117 {"quit", 0, (test_callback_fn
)quit_test
},
6118 {"mget", 1, (test_callback_fn
)mget_test
},
6119 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6120 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6121 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6122 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6123 {"mget_end", 0, (test_callback_fn
)mget_end
},
6124 {"get_stats", 0, (test_callback_fn
)get_stats
},
6125 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6126 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6127 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6128 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6129 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6130 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6131 {"read_through", 1, (test_callback_fn
)read_through
},
6132 {"delete_through", 1, (test_callback_fn
)delete_through
},
6133 {"noreply", 1, (test_callback_fn
)noreply_test
},
6134 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6135 #ifdef HAVE_LIBMEMCACHEDUTIL
6136 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6137 {"ping", 1, (test_callback_fn
)ping_test
},
6138 {"util_version", 1, (test_callback_fn
)util_version_test
},
6140 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6141 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6142 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6143 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6147 test_st behavior_tests
[] ={
6148 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6149 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6150 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6151 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6155 test_st regression_binary_vs_block
[] ={
6156 {"block add", 1, (test_callback_fn
)block_add_regression
},
6157 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6161 test_st async_tests
[] ={
6162 {"add", 1, (test_callback_fn
)add_wrapper
},
6166 test_st string_tests
[] ={
6167 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6168 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6169 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6170 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6171 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6172 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6173 {0, 0, (test_callback_fn
)0}
6176 test_st result_tests
[] ={
6177 {"result static", 0, (test_callback_fn
)result_static
},
6178 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6179 {0, 0, (test_callback_fn
)0}
6182 test_st version_1_2_3
[] ={
6183 {"append", 0, (test_callback_fn
)append_test
},
6184 {"prepend", 0, (test_callback_fn
)prepend_test
},
6185 {"cas", 0, (test_callback_fn
)cas_test
},
6186 {"cas2", 0, (test_callback_fn
)cas2_test
},
6187 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6188 {0, 0, (test_callback_fn
)0}
6191 test_st user_tests
[] ={
6192 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6193 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6194 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6195 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6196 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6197 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6198 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6199 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6200 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6201 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6202 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6203 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6204 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6205 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6206 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6207 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6210 ** It seems to be something weird with the character sets..
6211 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6212 ** guess I need to find out how this is supposed to work.. Perhaps I need
6213 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6214 ** so just disable the code for now...).
6216 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6218 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6219 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6220 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6221 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6222 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6223 {0, 0, (test_callback_fn
)0}
6226 test_st replication_tests
[]= {
6227 {"set", 1, (test_callback_fn
)replication_set_test
},
6228 {"get", 0, (test_callback_fn
)replication_get_test
},
6229 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6230 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6231 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6232 {0, 0, (test_callback_fn
)0}
6236 * The following test suite is used to verify that we don't introduce
6237 * regression bugs. If you want more information about the bug / test,
6238 * you should look in the bug report at
6239 * http://bugs.launchpad.net/libmemcached
6241 test_st regression_tests
[]= {
6242 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6243 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6244 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6245 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6246 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6247 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6248 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6249 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6250 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6251 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6252 {0, 0, (test_callback_fn
)0}
6255 test_st sasl_auth_tests
[]= {
6256 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6257 {0, 0, (test_callback_fn
)0}
6260 test_st ketama_compatibility
[]= {
6261 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6262 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6263 {0, 0, (test_callback_fn
)0}
6266 test_st generate_tests
[] ={
6267 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6268 {"generate_data", 1, (test_callback_fn
)generate_data
},
6269 {"get_read", 0, (test_callback_fn
)get_read
},
6270 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6271 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6272 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6273 {"generate_data", 1, (test_callback_fn
)generate_data
},
6274 {"mget_read", 0, (test_callback_fn
)mget_read
},
6275 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6276 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6277 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6278 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6279 {"generate_data", 1, (test_callback_fn
)generate_data
},
6280 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6281 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6282 {0, 0, (test_callback_fn
)0}
6285 test_st consistent_tests
[] ={
6286 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6287 {"generate_data", 1, (test_callback_fn
)generate_data
},
6288 {"get_read", 0, (test_callback_fn
)get_read_count
},
6289 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6290 {0, 0, (test_callback_fn
)0}
6293 test_st consistent_weighted_tests
[] ={
6294 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6295 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6296 {"get_read", 0, (test_callback_fn
)get_read_count
},
6297 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6298 {0, 0, (test_callback_fn
)0}
6301 test_st hsieh_availability
[] ={
6302 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6303 {0, 0, (test_callback_fn
)0}
6307 test_st hash_sanity
[] ={
6308 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6309 {0, 0, (test_callback_fn
)0}
6313 test_st ketama_auto_eject_hosts
[] ={
6314 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6315 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6316 {0, 0, (test_callback_fn
)0}
6319 test_st hash_tests
[] ={
6320 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6321 {"md5", 0, (test_callback_fn
)md5_run
},
6322 {"crc", 0, (test_callback_fn
)crc_run
},
6323 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6324 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6325 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6326 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6327 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6328 {"murmur", 0, (test_callback_fn
)murmur_run
},
6329 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6330 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6331 {0, 0, (test_callback_fn
)0}
6334 test_st error_conditions
[] ={
6335 {"memcached_get_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn
)memcached_get_MEMCACHED_SOME_ERRORS
},
6336 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6337 {"memcached_get_by_key_MEMCACHED_SOME_ERRORS", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_SOME_ERRORS
},
6338 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6339 {0, 0, (test_callback_fn
)0}
6342 collection_st collection
[] ={
6344 {"hash_sanity", 0, 0, hash_sanity
},
6346 {"hsieh_availability", 0, 0, hsieh_availability
},
6347 {"block", 0, 0, tests
},
6348 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6349 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6350 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6351 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6352 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6353 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6354 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6355 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6356 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6357 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6358 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6359 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6360 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6361 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6362 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6363 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6364 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6365 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6366 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6367 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6368 #ifdef MEMCACHED_ENABLE_DEPRECATED
6369 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6371 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6372 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6373 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6374 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6375 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6376 {"string", 0, 0, string_tests
},
6377 {"result", 0, 0, result_tests
},
6378 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6379 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6380 {"user", 0, 0, user_tests
},
6381 {"generate", 0, 0, generate_tests
},
6382 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6383 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6384 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6385 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6386 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6387 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6388 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6390 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6391 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6392 {"consistent_not", 0, 0, consistent_tests
},
6393 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6394 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6395 {"ketama_compat", 0, 0, ketama_compatibility
},
6396 {"test_hashes", 0, 0, hash_tests
},
6397 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6398 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6399 {"regression", 0, 0, regression_tests
},
6400 {"behaviors", 0, 0, behavior_tests
},
6401 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6402 {"error_conditions", 0, 0, error_conditions
},
6406 #define SERVERS_TO_CREATE 5
6408 #include "libmemcached_world.h"
6410 void get_world(world_st
*world
)
6412 world
->collections
= collection
;
6414 world
->create
= (test_callback_create_fn
)world_create
;
6415 world
->destroy
= (test_callback_fn
)world_destroy
;
6417 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6418 world
->test
.flush
= (test_callback_fn
)world_flush
;
6419 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6420 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6421 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6423 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6424 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6426 world
->runner
= &defualt_libmemcached_runner
;