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
)
685 unsigned int max
= 10000;
693 for (uint32_t x
= 0; x
< max
; x
++)
699 static test_return_t
replace_test(memcached_st
*memc
)
701 memcached_return_t rc
;
702 const char *key
= "foo";
703 const char *value
= "when we sanitize";
704 const char *original
= "first we insert some data";
706 rc
= memcached_set(memc
, key
, strlen(key
),
707 original
, strlen(original
),
708 (time_t)0, (uint32_t)0);
709 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
711 rc
= memcached_replace(memc
, key
, strlen(key
),
712 value
, strlen(value
),
713 (time_t)0, (uint32_t)0);
714 test_true(rc
== MEMCACHED_SUCCESS
);
719 static test_return_t
delete_test(memcached_st
*memc
)
721 memcached_return_t rc
;
722 const char *key
= "foo";
723 const char *value
= "when we sanitize";
725 rc
= memcached_set(memc
, key
, strlen(key
),
726 value
, strlen(value
),
727 (time_t)0, (uint32_t)0);
728 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
730 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
731 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
736 static test_return_t
flush_test(memcached_st
*memc
)
738 memcached_return_t rc
;
740 rc
= memcached_flush(memc
, 0);
741 test_true(rc
== MEMCACHED_SUCCESS
);
746 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
747 const memcached_server_st
*server
__attribute__((unused
)),
748 void *context
__attribute__((unused
)))
752 return MEMCACHED_SUCCESS
;
755 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
758 strcpy(context
, "foo bad");
759 memcached_server_fn callbacks
[1];
761 callbacks
[0]= server_function
;
762 memcached_server_cursor(memc
, callbacks
, context
, 1);
766 static test_return_t
bad_key_test(memcached_st
*memc
)
768 memcached_return_t rc
;
769 const char *key
= "foo bad";
771 size_t string_length
;
773 memcached_st
*memc_clone
;
775 size_t max_keylen
= 0xffff;
777 // Just skip if we are in binary mode.
778 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
781 memc_clone
= memcached_clone(NULL
, memc
);
782 test_true(memc_clone
);
784 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
785 test_true(rc
== MEMCACHED_SUCCESS
);
787 /* All keys are valid in the binary protocol (except for length) */
788 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
790 string
= memcached_get(memc_clone
, key
, strlen(key
),
791 &string_length
, &flags
, &rc
);
792 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
793 test_true(string_length
== 0);
797 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
798 test_true(rc
== MEMCACHED_SUCCESS
);
799 string
= memcached_get(memc_clone
, key
, strlen(key
),
800 &string_length
, &flags
, &rc
);
801 test_true(rc
== MEMCACHED_NOTFOUND
);
802 test_true(string_length
== 0);
805 /* Test multi key for bad keys */
806 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
807 size_t key_lengths
[] = { 7, 7, 7 };
809 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
810 test_true(rc
== MEMCACHED_SUCCESS
);
812 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
813 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
815 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
816 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
820 /* The following test should be moved to the end of this function when the
821 memcached server is updated to allow max size length of the keys in the
824 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
825 test_true(rc
== MEMCACHED_SUCCESS
);
827 char *longkey
= malloc(max_keylen
+ 1);
830 memset(longkey
, 'a', max_keylen
+ 1);
831 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
832 &string_length
, &flags
, &rc
);
833 test_true(rc
== MEMCACHED_NOTFOUND
);
834 test_true(string_length
== 0);
837 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
838 &string_length
, &flags
, &rc
);
839 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
840 test_true(string_length
== 0);
847 /* Make sure zero length keys are marked as bad */
849 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
850 test_true(rc
== MEMCACHED_SUCCESS
);
851 string
= memcached_get(memc_clone
, key
, 0,
852 &string_length
, &flags
, &rc
);
853 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
854 test_true(string_length
== 0);
857 memcached_free(memc_clone
);
862 #define READ_THROUGH_VALUE "set for me"
863 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
864 char *key
__attribute__((unused
)),
865 size_t key_length
__attribute__((unused
)),
866 memcached_result_st
*result
)
869 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
872 static test_return_t
read_through(memcached_st
*memc
)
874 memcached_return_t rc
;
875 const char *key
= "foo";
877 size_t string_length
;
879 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
881 string
= memcached_get(memc
, key
, strlen(key
),
882 &string_length
, &flags
, &rc
);
884 test_true(rc
== MEMCACHED_NOTFOUND
);
885 test_false(string_length
);
888 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
890 test_true(rc
== MEMCACHED_SUCCESS
);
892 string
= memcached_get(memc
, key
, strlen(key
),
893 &string_length
, &flags
, &rc
);
895 test_true(rc
== MEMCACHED_SUCCESS
);
896 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
897 test_strcmp(READ_THROUGH_VALUE
, string
);
900 string
= memcached_get(memc
, key
, strlen(key
),
901 &string_length
, &flags
, &rc
);
903 test_true(rc
== MEMCACHED_SUCCESS
);
904 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
905 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
911 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
913 size_t key_length
__attribute__((unused
)))
917 return MEMCACHED_SUCCESS
;
920 static test_return_t
delete_through(memcached_st
*memc
)
922 memcached_trigger_delete_key_fn callback
;
923 memcached_return_t rc
;
925 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
927 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
928 test_true(rc
== MEMCACHED_SUCCESS
);
933 static test_return_t
get_test(memcached_st
*memc
)
935 memcached_return_t rc
;
936 const char *key
= "foo";
938 size_t string_length
;
941 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
942 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
944 string
= memcached_get(memc
, key
, strlen(key
),
945 &string_length
, &flags
, &rc
);
947 test_true(rc
== MEMCACHED_NOTFOUND
);
948 test_false(string_length
);
954 static test_return_t
get_test2(memcached_st
*memc
)
956 memcached_return_t rc
;
957 const char *key
= "foo";
958 const char *value
= "when we sanitize";
960 size_t string_length
;
963 rc
= memcached_set(memc
, key
, strlen(key
),
964 value
, strlen(value
),
965 (time_t)0, (uint32_t)0);
966 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
968 string
= memcached_get(memc
, key
, strlen(key
),
969 &string_length
, &flags
, &rc
);
972 test_true(rc
== MEMCACHED_SUCCESS
);
973 test_true(string_length
== strlen(value
));
974 test_true(!memcmp(string
, value
, string_length
));
981 static test_return_t
set_test2(memcached_st
*memc
)
983 memcached_return_t rc
;
984 const char *key
= "foo";
985 const char *value
= "train in the brain";
986 size_t value_length
= strlen(value
);
989 for (x
= 0; x
< 10; x
++)
991 rc
= memcached_set(memc
, key
, strlen(key
),
993 (time_t)0, (uint32_t)0);
994 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1000 static test_return_t
set_test3(memcached_st
*memc
)
1002 memcached_return_t rc
;
1004 size_t value_length
= 8191;
1007 value
= (char*)malloc(value_length
);
1010 for (x
= 0; x
< value_length
; x
++)
1011 value
[x
] = (char) (x
% 127);
1013 /* The dump test relies on there being at least 32 items in memcached */
1014 for (x
= 0; x
< 32; x
++)
1018 sprintf(key
, "foo%u", x
);
1020 rc
= memcached_set(memc
, key
, strlen(key
),
1021 value
, value_length
,
1022 (time_t)0, (uint32_t)0);
1023 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1028 return TEST_SUCCESS
;
1031 static test_return_t
get_test3(memcached_st
*memc
)
1033 memcached_return_t rc
;
1034 const char *key
= "foo";
1036 size_t value_length
= 8191;
1038 size_t string_length
;
1042 value
= (char*)malloc(value_length
);
1045 for (x
= 0; x
< value_length
; x
++)
1046 value
[x
] = (char) (x
% 127);
1048 rc
= memcached_set(memc
, key
, strlen(key
),
1049 value
, value_length
,
1050 (time_t)0, (uint32_t)0);
1051 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1053 string
= memcached_get(memc
, key
, strlen(key
),
1054 &string_length
, &flags
, &rc
);
1056 test_true(rc
== MEMCACHED_SUCCESS
);
1058 test_true(string_length
== value_length
);
1059 test_true(!memcmp(string
, value
, string_length
));
1064 return TEST_SUCCESS
;
1067 static test_return_t
get_test4(memcached_st
*memc
)
1069 memcached_return_t rc
;
1070 const char *key
= "foo";
1072 size_t value_length
= 8191;
1074 size_t string_length
;
1078 value
= (char*)malloc(value_length
);
1081 for (x
= 0; x
< value_length
; x
++)
1082 value
[x
] = (char) (x
% 127);
1084 rc
= memcached_set(memc
, key
, strlen(key
),
1085 value
, value_length
,
1086 (time_t)0, (uint32_t)0);
1087 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1089 for (x
= 0; x
< 10; x
++)
1091 string
= memcached_get(memc
, key
, strlen(key
),
1092 &string_length
, &flags
, &rc
);
1094 test_true(rc
== MEMCACHED_SUCCESS
);
1096 test_true(string_length
== value_length
);
1097 test_true(!memcmp(string
, value
, string_length
));
1103 return TEST_SUCCESS
;
1107 * This test verifies that memcached_read_one_response doesn't try to
1108 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1109 * responses before you execute a storage command.
1111 static test_return_t
get_test5(memcached_st
*memc
)
1114 ** Request the same key twice, to ensure that we hash to the same server
1115 ** (so that we have multiple response values queued up) ;-)
1117 const char *keys
[]= { "key", "key" };
1118 size_t lengths
[]= { 3, 3 };
1122 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1123 keys
[0], lengths
[0], 0, 0);
1124 test_true(rc
== MEMCACHED_SUCCESS
);
1125 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1127 memcached_result_st results_obj
;
1128 memcached_result_st
*results
;
1129 results
=memcached_result_create(memc
, &results_obj
);
1131 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1133 memcached_result_free(&results_obj
);
1135 /* Don't read out the second result, but issue a set instead.. */
1136 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1137 test_true(rc
== MEMCACHED_SUCCESS
);
1139 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1140 &rlen
, &flags
, &rc
);
1141 test_true(val
== NULL
);
1142 test_true(rc
== MEMCACHED_NOTFOUND
);
1143 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1144 test_true(val
!= NULL
);
1145 test_true(rc
== MEMCACHED_SUCCESS
);
1148 return TEST_SUCCESS
;
1151 static test_return_t
mget_end(memcached_st
*memc
)
1153 const char *keys
[]= { "foo", "foo2" };
1154 size_t lengths
[]= { 3, 4 };
1155 const char *values
[]= { "fjord", "41" };
1157 memcached_return_t rc
;
1160 for (int i
= 0; i
< 2; i
++)
1162 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1163 (time_t)0, (uint32_t)0);
1164 test_true(rc
== MEMCACHED_SUCCESS
);
1168 size_t string_length
;
1171 // retrieve both via mget
1172 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1173 test_true(rc
== MEMCACHED_SUCCESS
);
1175 char key
[MEMCACHED_MAX_KEY
];
1178 // this should get both
1179 for (int i
= 0; i
< 2; i
++)
1181 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1183 test_true(rc
== MEMCACHED_SUCCESS
);
1185 if (key_length
== 4)
1187 test_true(string_length
== strlen(values
[val
]));
1188 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1192 // this should indicate end
1193 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1194 test_true(rc
== MEMCACHED_END
);
1197 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1198 test_true(rc
== MEMCACHED_SUCCESS
);
1200 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1201 test_true(key_length
== lengths
[0]);
1202 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1203 test_true(string_length
== strlen(values
[0]));
1204 test_true(strncmp(values
[0], string
, string_length
) == 0);
1205 test_true(rc
== MEMCACHED_SUCCESS
);
1208 // this should indicate end
1209 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1210 test_true(rc
== MEMCACHED_END
);
1212 return TEST_SUCCESS
;
1215 /* Do not copy the style of this code, I just access hosts to testthis function */
1216 static test_return_t
stats_servername_test(memcached_st
*memc
)
1218 memcached_return_t rc
;
1219 memcached_stat_st memc_stat
;
1220 memcached_server_instance_st instance
=
1221 memcached_server_instance_by_position(memc
, 0);
1223 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1224 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1225 return TEST_SKIPPED
;
1227 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1228 memcached_server_name(instance
),
1229 memcached_server_port(instance
));
1231 return TEST_SUCCESS
;
1234 static test_return_t
increment_test(memcached_st
*memc
)
1236 uint64_t new_number
;
1237 memcached_return_t rc
;
1238 const char *key
= "number";
1239 const char *value
= "0";
1241 rc
= memcached_set(memc
, key
, strlen(key
),
1242 value
, strlen(value
),
1243 (time_t)0, (uint32_t)0);
1244 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1246 rc
= memcached_increment(memc
, key
, strlen(key
),
1248 test_true(rc
== MEMCACHED_SUCCESS
);
1249 test_true(new_number
== 1);
1251 rc
= memcached_increment(memc
, key
, strlen(key
),
1253 test_true(rc
== MEMCACHED_SUCCESS
);
1254 test_true(new_number
== 2);
1256 return TEST_SUCCESS
;
1259 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1261 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1263 uint64_t new_number
;
1264 memcached_return_t rc
;
1265 const char *key
= "number";
1266 uint64_t initial
= 0;
1268 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1269 1, initial
, 0, &new_number
);
1270 test_true(rc
== MEMCACHED_SUCCESS
);
1271 test_true(new_number
== initial
);
1273 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1274 1, initial
, 0, &new_number
);
1275 test_true(rc
== MEMCACHED_SUCCESS
);
1276 test_true(new_number
== (initial
+ 1));
1278 return TEST_SUCCESS
;
1281 static test_return_t
decrement_test(memcached_st
*memc
)
1283 uint64_t new_number
;
1284 memcached_return_t rc
;
1285 const char *key
= "number";
1286 const char *value
= "3";
1288 rc
= memcached_set(memc
, key
, strlen(key
),
1289 value
, strlen(value
),
1290 (time_t)0, (uint32_t)0);
1291 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1293 rc
= memcached_decrement(memc
, key
, strlen(key
),
1295 test_true(rc
== MEMCACHED_SUCCESS
);
1296 test_true(new_number
== 2);
1298 rc
= memcached_decrement(memc
, key
, strlen(key
),
1300 test_true(rc
== MEMCACHED_SUCCESS
);
1301 test_true(new_number
== 1);
1303 return TEST_SUCCESS
;
1306 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1308 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1310 uint64_t new_number
;
1311 memcached_return_t rc
;
1312 const char *key
= "number";
1313 uint64_t initial
= 3;
1315 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1316 1, initial
, 0, &new_number
);
1317 test_true(rc
== MEMCACHED_SUCCESS
);
1318 test_true(new_number
== initial
);
1320 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1321 1, initial
, 0, &new_number
);
1322 test_true(rc
== MEMCACHED_SUCCESS
);
1323 test_true(new_number
== (initial
- 1));
1325 return TEST_SUCCESS
;
1328 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1330 uint64_t new_number
;
1331 memcached_return_t rc
;
1332 const char *master_key
= "foo";
1333 const char *key
= "number";
1334 const char *value
= "0";
1336 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1338 value
, strlen(value
),
1339 (time_t)0, (uint32_t)0);
1340 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1342 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1344 test_true(rc
== MEMCACHED_SUCCESS
);
1345 test_true(new_number
== 1);
1347 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1349 test_true(rc
== MEMCACHED_SUCCESS
);
1350 test_true(new_number
== 2);
1352 return TEST_SUCCESS
;
1355 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1357 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1359 uint64_t new_number
;
1360 memcached_return_t rc
;
1361 const char *master_key
= "foo";
1362 const char *key
= "number";
1363 uint64_t initial
= 0;
1365 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1367 1, initial
, 0, &new_number
);
1368 test_true(rc
== MEMCACHED_SUCCESS
);
1369 test_true(new_number
== initial
);
1371 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1373 1, initial
, 0, &new_number
);
1374 test_true(rc
== MEMCACHED_SUCCESS
);
1375 test_true(new_number
== (initial
+ 1));
1377 return TEST_SUCCESS
;
1380 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1382 uint64_t new_number
;
1383 memcached_return_t rc
;
1384 const char *master_key
= "foo";
1385 const char *key
= "number";
1386 const char *value
= "3";
1388 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1390 value
, strlen(value
),
1391 (time_t)0, (uint32_t)0);
1392 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1394 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1397 test_true(rc
== MEMCACHED_SUCCESS
);
1398 test_true(new_number
== 2);
1400 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1403 test_true(rc
== MEMCACHED_SUCCESS
);
1404 test_true(new_number
== 1);
1406 return TEST_SUCCESS
;
1409 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1411 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1413 uint64_t new_number
;
1414 memcached_return_t rc
;
1415 const char *master_key
= "foo";
1416 const char *key
= "number";
1417 uint64_t initial
= 3;
1419 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1421 1, initial
, 0, &new_number
);
1422 test_true(rc
== MEMCACHED_SUCCESS
);
1423 test_true(new_number
== initial
);
1425 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1427 1, initial
, 0, &new_number
);
1428 test_true(rc
== MEMCACHED_SUCCESS
);
1429 test_true(new_number
== (initial
- 1));
1431 return TEST_SUCCESS
;
1434 static test_return_t
quit_test(memcached_st
*memc
)
1436 memcached_return_t rc
;
1437 const char *key
= "fudge";
1438 const char *value
= "sanford and sun";
1440 rc
= memcached_set(memc
, key
, strlen(key
),
1441 value
, strlen(value
),
1442 (time_t)10, (uint32_t)3);
1443 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1444 memcached_quit(memc
);
1446 rc
= memcached_set(memc
, key
, strlen(key
),
1447 value
, strlen(value
),
1448 (time_t)50, (uint32_t)9);
1449 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1451 return TEST_SUCCESS
;
1454 static test_return_t
mget_result_test(memcached_st
*memc
)
1456 memcached_return_t rc
;
1457 const char *keys
[]= {"fudge", "son", "food"};
1458 size_t key_length
[]= {5, 3, 4};
1461 memcached_result_st results_obj
;
1462 memcached_result_st
*results
;
1464 results
= memcached_result_create(memc
, &results_obj
);
1466 test_true(&results_obj
== results
);
1468 /* We need to empty the server before continueing test */
1469 rc
= memcached_flush(memc
, 0);
1470 test_true(rc
== MEMCACHED_SUCCESS
);
1472 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1473 test_true(rc
== MEMCACHED_SUCCESS
);
1475 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1480 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1481 test_true(!results
);
1482 test_true(rc
== MEMCACHED_END
);
1484 for (x
= 0; x
< 3; x
++)
1486 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1487 keys
[x
], key_length
[x
],
1488 (time_t)50, (uint32_t)9);
1489 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1492 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1493 test_true(rc
== MEMCACHED_SUCCESS
);
1495 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1498 test_true(&results_obj
== results
);
1499 test_true(rc
== MEMCACHED_SUCCESS
);
1500 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1501 test_true(!memcmp(memcached_result_key_value(results
),
1502 memcached_result_value(results
),
1503 memcached_result_length(results
)));
1506 memcached_result_free(&results_obj
);
1508 return TEST_SUCCESS
;
1511 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1513 memcached_return_t rc
;
1514 const char *keys
[]= {"fudge", "son", "food"};
1515 size_t key_length
[]= {5, 3, 4};
1518 memcached_result_st
*results
;
1520 /* We need to empty the server before continueing test */
1521 rc
= memcached_flush(memc
, 0);
1522 test_true(rc
== MEMCACHED_SUCCESS
);
1524 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1525 test_true(rc
== MEMCACHED_SUCCESS
);
1527 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1531 test_true(!results
);
1532 test_true(rc
== MEMCACHED_END
);
1534 for (x
= 0; x
< 3; x
++)
1536 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1537 keys
[x
], key_length
[x
],
1538 (time_t)50, (uint32_t)9);
1539 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1542 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1543 test_true(rc
== MEMCACHED_SUCCESS
);
1546 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1549 test_true(rc
== MEMCACHED_SUCCESS
);
1550 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1551 test_true(!memcmp(memcached_result_key_value(results
),
1552 memcached_result_value(results
),
1553 memcached_result_length(results
)));
1554 memcached_result_free(results
);
1558 return TEST_SUCCESS
;
1561 /* Count the results */
1562 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1563 memcached_result_st
*result
__attribute__((unused
)),
1566 size_t *counter
= (size_t *)context
;
1568 *counter
= *counter
+ 1;
1570 return MEMCACHED_SUCCESS
;
1573 static test_return_t
mget_result_function(memcached_st
*memc
)
1575 memcached_return_t rc
;
1576 const char *keys
[]= {"fudge", "son", "food"};
1577 size_t key_length
[]= {5, 3, 4};
1580 memcached_execute_fn callbacks
[1];
1582 /* We need to empty the server before continueing test */
1583 rc
= memcached_flush(memc
, 0);
1584 for (x
= 0; x
< 3; x
++)
1586 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1587 keys
[x
], key_length
[x
],
1588 (time_t)50, (uint32_t)9);
1589 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1592 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1593 test_true(rc
== MEMCACHED_SUCCESS
);
1595 callbacks
[0]= &callback_counter
;
1597 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1599 test_true(counter
== 3);
1601 return TEST_SUCCESS
;
1604 static test_return_t
mget_test(memcached_st
*memc
)
1606 memcached_return_t rc
;
1607 const char *keys
[]= {"fudge", "son", "food"};
1608 size_t key_length
[]= {5, 3, 4};
1612 char return_key
[MEMCACHED_MAX_KEY
];
1613 size_t return_key_length
;
1615 size_t return_value_length
;
1617 /* We need to empty the server before continueing test */
1618 rc
= memcached_flush(memc
, 0);
1619 test_true(rc
== MEMCACHED_SUCCESS
);
1621 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1622 test_true(rc
== MEMCACHED_SUCCESS
);
1624 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1625 &return_value_length
, &flags
, &rc
)) != NULL
)
1627 test_true(return_value
);
1629 test_true(!return_value
);
1630 test_true(return_value_length
== 0);
1631 test_true(rc
== MEMCACHED_END
);
1633 for (x
= 0; x
< 3; x
++)
1635 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1636 keys
[x
], key_length
[x
],
1637 (time_t)50, (uint32_t)9);
1638 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1641 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1642 test_true(rc
== MEMCACHED_SUCCESS
);
1645 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1646 &return_value_length
, &flags
, &rc
)))
1648 test_true(return_value
);
1649 test_true(rc
== MEMCACHED_SUCCESS
);
1650 test_true(return_key_length
== return_value_length
);
1651 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1656 return TEST_SUCCESS
;
1659 static test_return_t
mget_execute(memcached_st
*memc
)
1663 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1667 * I only want to hit _one_ server so I know the number of requests I'm
1668 * sending in the pipeline.
1670 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1671 memc
->number_of_hosts
= 1;
1673 size_t max_keys
= 20480;
1676 char **keys
= calloc(max_keys
, sizeof(char*));
1677 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1679 /* First add all of the items.. */
1680 char blob
[1024] = {0};
1681 memcached_return_t rc
;
1683 for (size_t x
= 0; x
< max_keys
; ++x
)
1687 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
1689 test_true(keys
[x
] != NULL
);
1690 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1691 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1694 /* Try to get all of them with a large multiget */
1696 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1697 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1698 max_keys
, callbacks
, &counter
, 1);
1700 if (rc
== MEMCACHED_SUCCESS
)
1703 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1704 test_true(rc
== MEMCACHED_END
);
1706 /* Verify that we got all of the items */
1707 test_true(counter
== max_keys
);
1709 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1711 test_true(counter
== 0);
1715 test_fail("note: this test functions differently when in binary mode");
1718 /* Release all allocated resources */
1719 for (size_t x
= 0; x
< max_keys
; ++x
)
1726 memc
->number_of_hosts
= number_of_hosts
;
1727 return TEST_SUCCESS
;
1730 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1732 static test_return_t
key_setup(memcached_st
*memc
)
1736 if (pre_binary(memc
) != TEST_SUCCESS
)
1737 return TEST_SKIPPED
;
1739 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1741 return TEST_SUCCESS
;
1744 static test_return_t
key_teardown(memcached_st
*memc
)
1747 pairs_free(global_pairs
);
1749 return TEST_SUCCESS
;
1752 static test_return_t
block_add_regression(memcached_st
*memc
)
1754 /* First add all of the items.. */
1755 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1757 memcached_return_t rc
;
1758 char blob
[1024] = {0};
1760 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1761 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1764 return TEST_SUCCESS
;
1767 static test_return_t
binary_add_regression(memcached_st
*memc
)
1769 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1770 test_return_t rc
= block_add_regression(memc
);
1771 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1775 static test_return_t
get_stats_keys(memcached_st
*memc
)
1779 memcached_stat_st memc_stat
;
1780 memcached_return_t rc
;
1782 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1783 test_true(rc
== MEMCACHED_SUCCESS
);
1784 for (ptr
= stat_list
; *ptr
; ptr
++)
1789 return TEST_SUCCESS
;
1792 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1794 const char *version_string
;
1796 version_string
= memcached_lib_version();
1798 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1800 return TEST_SUCCESS
;
1803 static test_return_t
get_stats(memcached_st
*memc
)
1807 memcached_return_t rc
;
1808 memcached_stat_st
*memc_stat
;
1810 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1811 test_true(rc
== MEMCACHED_SUCCESS
);
1813 test_true(rc
== MEMCACHED_SUCCESS
);
1814 test_true(memc_stat
);
1816 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1818 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1819 test_true(rc
== MEMCACHED_SUCCESS
);
1820 for (ptr
= stat_list
; *ptr
; ptr
++);
1825 memcached_stat_free(NULL
, memc_stat
);
1827 return TEST_SUCCESS
;
1830 static test_return_t
add_host_test(memcached_st
*memc
)
1833 memcached_server_st
*servers
;
1834 memcached_return_t rc
;
1835 char servername
[]= "0.example.com";
1837 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1839 test_true(1 == memcached_server_list_count(servers
));
1841 for (x
= 2; x
< 20; x
++)
1843 char buffer
[SMALL_STRING_LEN
];
1845 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1846 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1848 test_true(rc
== MEMCACHED_SUCCESS
);
1849 test_true(x
== memcached_server_list_count(servers
));
1852 rc
= memcached_server_push(memc
, servers
);
1853 test_true(rc
== MEMCACHED_SUCCESS
);
1854 rc
= memcached_server_push(memc
, servers
);
1855 test_true(rc
== MEMCACHED_SUCCESS
);
1857 memcached_server_list_free(servers
);
1859 return TEST_SUCCESS
;
1862 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1864 return MEMCACHED_SUCCESS
;
1867 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1869 return MEMCACHED_SUCCESS
;
1872 static test_return_t
callback_test(memcached_st
*memc
)
1874 /* Test User Data */
1878 memcached_return_t rc
;
1880 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1881 test_true(rc
== MEMCACHED_SUCCESS
);
1882 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1883 test_true(*test_ptr
== x
);
1886 /* Test Clone Callback */
1888 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1889 void *clone_cb_ptr
= *(void **)&clone_cb
;
1890 void *temp_function
= NULL
;
1891 memcached_return_t rc
;
1893 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1895 test_true(rc
== MEMCACHED_SUCCESS
);
1896 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1897 test_true(temp_function
== clone_cb_ptr
);
1900 /* Test Cleanup Callback */
1902 memcached_cleanup_fn cleanup_cb
=
1903 (memcached_cleanup_fn
)cleanup_test_callback
;
1904 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1905 void *temp_function
= NULL
;
1906 memcached_return_t rc
;
1908 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1910 test_true(rc
== MEMCACHED_SUCCESS
);
1911 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1912 test_true(temp_function
== cleanup_cb_ptr
);
1915 return TEST_SUCCESS
;
1918 /* We don't test the behavior itself, we test the switches */
1919 static test_return_t
behavior_test(memcached_st
*memc
)
1924 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1925 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1926 test_true(value
== 1);
1928 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1929 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1930 test_true(value
== 1);
1932 set
= MEMCACHED_HASH_MD5
;
1933 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1934 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1935 test_true(value
== MEMCACHED_HASH_MD5
);
1939 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1940 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1941 test_true(value
== 0);
1943 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1944 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1945 test_true(value
== 0);
1947 set
= MEMCACHED_HASH_DEFAULT
;
1948 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1949 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1950 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1952 set
= MEMCACHED_HASH_CRC
;
1953 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1954 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1955 test_true(value
== MEMCACHED_HASH_CRC
);
1957 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1958 test_true(value
> 0);
1960 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1961 test_true(value
> 0);
1963 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1965 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1967 return TEST_SUCCESS
;
1970 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1972 memcached_return_t rc
;
1976 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1977 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1979 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1981 if (rc
== MEMCACHED_SUCCESS
)
1983 test_true((bool)value
== set
);
1987 test_false((bool)value
== set
);
1990 return TEST_SUCCESS
;
1994 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
1996 memcached_return_t rc
;
2000 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2001 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2003 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2005 if (rc
== MEMCACHED_SUCCESS
)
2007 test_true((bool)value
== set
);
2011 test_false((bool)value
== set
);
2014 return TEST_SUCCESS
;
2018 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2020 memcached_return_t rc
;
2024 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2025 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2027 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2029 if (rc
== MEMCACHED_SUCCESS
)
2031 test_true((bool)value
== set
);
2035 test_false((bool)value
== set
);
2038 return TEST_SUCCESS
;
2041 static test_return_t
fetch_all_results(memcached_st
*memc
)
2043 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2044 char return_key
[MEMCACHED_MAX_KEY
];
2045 size_t return_key_length
;
2047 size_t return_value_length
;
2050 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2051 &return_value_length
, &flags
, &rc
)))
2053 test_true(return_value
);
2054 test_true(rc
== MEMCACHED_SUCCESS
);
2058 return ((rc
== MEMCACHED_END
) || (rc
== MEMCACHED_SUCCESS
)) ? TEST_SUCCESS
: TEST_FAILURE
;
2061 /* Test case provided by Cal Haldenbrand */
2062 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2064 unsigned int setter
= 1;
2066 unsigned long long total
= 0;
2069 char randomstuff
[6 * 1024];
2070 memcached_return_t rc
;
2072 memset(randomstuff
, 0, 6 * 1024);
2074 /* We just keep looking at the same values over and over */
2077 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2078 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2082 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2086 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2087 memset(randomstuff
, 0, 6 * 1024);
2088 test_true(size
< 6 * 1024); /* Being safe here */
2090 for (j
= 0 ; j
< size
;j
++)
2091 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2094 snprintf(key
, sizeof(key
), "%u", x
);
2095 rc
= memcached_set(memc
, key
, strlen(key
),
2096 randomstuff
, strlen(randomstuff
), 10, 0);
2097 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2098 /* If we fail, lets try again */
2099 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2100 rc
= memcached_set(memc
, key
, strlen(key
),
2101 randomstuff
, strlen(randomstuff
), 10, 0);
2102 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2105 return TEST_SUCCESS
;
2108 /* Test case provided by Cal Haldenbrand */
2109 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2111 unsigned int setter
;
2115 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2116 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2118 setter
= 20 * 1024576;
2119 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2120 setter
= 20 * 1024576;
2121 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2122 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2123 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2125 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2128 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2130 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2131 char buffer
[SMALL_STRING_LEN
];
2136 memset(buffer
, 0, SMALL_STRING_LEN
);
2138 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2139 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2140 &val_len
, &flags
, &rc
);
2141 if (rc
!= MEMCACHED_SUCCESS
)
2143 if (rc
== MEMCACHED_NOTFOUND
)
2157 return TEST_SUCCESS
;
2160 /* Do a large mget() over all the keys we think exist */
2161 #define KEY_COUNT 3000 // * 1024576
2162 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2164 memcached_return_t rc
;
2165 unsigned int setter
;
2168 size_t key_lengths
[KEY_COUNT
];
2171 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2172 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2174 setter
= 20 * 1024576;
2175 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2176 setter
= 20 * 1024576;
2177 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2178 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2179 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2182 keys
= calloc(KEY_COUNT
, sizeof(char *));
2184 for (x
= 0; x
< KEY_COUNT
; x
++)
2188 snprintf(buffer
, 30, "%u", x
);
2189 keys
[x
]= strdup(buffer
);
2190 key_lengths
[x
]= strlen(keys
[x
]);
2193 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2194 test_true(rc
== MEMCACHED_SUCCESS
);
2196 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2198 for (x
= 0; x
< KEY_COUNT
; x
++)
2202 return TEST_SUCCESS
;
2205 /* Make sure we behave properly if server list has no values */
2206 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2208 memcached_return_t rc
;
2209 const char *keys
[]= {"fudge", "son", "food"};
2210 size_t key_length
[]= {5, 3, 4};
2213 char return_key
[MEMCACHED_MAX_KEY
];
2214 size_t return_key_length
;
2216 size_t return_value_length
;
2218 /* Here we free everything before running a bunch of mget tests */
2219 memcached_servers_reset(memc
);
2222 /* We need to empty the server before continueing test */
2223 rc
= memcached_flush(memc
, 0);
2224 test_true(rc
== MEMCACHED_NO_SERVERS
);
2226 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2227 test_true(rc
== MEMCACHED_NO_SERVERS
);
2229 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2230 &return_value_length
, &flags
, &rc
)) != NULL
)
2232 test_true(return_value
);
2234 test_true(!return_value
);
2235 test_true(return_value_length
== 0);
2236 test_true(rc
== MEMCACHED_NO_SERVERS
);
2238 for (x
= 0; x
< 3; x
++)
2240 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2241 keys
[x
], key_length
[x
],
2242 (time_t)50, (uint32_t)9);
2243 test_true(rc
== MEMCACHED_NO_SERVERS
);
2246 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2247 test_true(rc
== MEMCACHED_NO_SERVERS
);
2250 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2251 &return_value_length
, &flags
, &rc
)))
2253 test_true(return_value
);
2254 test_true(rc
== MEMCACHED_SUCCESS
);
2255 test_true(return_key_length
== return_value_length
);
2256 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2261 return TEST_SUCCESS
;
2264 #define VALUE_SIZE_BUG5 1048064
2265 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2267 memcached_return_t rc
;
2268 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2269 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2270 char return_key
[MEMCACHED_MAX_KEY
];
2271 size_t return_key_length
;
2273 size_t value_length
;
2277 char insert_data
[VALUE_SIZE_BUG5
];
2279 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2280 insert_data
[x
]= (signed char)rand();
2282 memcached_flush(memc
, 0);
2283 value
= memcached_get(memc
, keys
[0], key_length
[0],
2284 &value_length
, &flags
, &rc
);
2285 test_true(value
== NULL
);
2286 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2289 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2290 &value_length
, &flags
, &rc
)))
2292 test_true(count
== 0);
2294 for (x
= 0; x
< 4; x
++)
2296 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2297 insert_data
, VALUE_SIZE_BUG5
,
2298 (time_t)0, (uint32_t)0);
2299 test_true(rc
== MEMCACHED_SUCCESS
);
2302 for (x
= 0; x
< 10; x
++)
2304 value
= memcached_get(memc
, keys
[0], key_length
[0],
2305 &value_length
, &flags
, &rc
);
2309 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2311 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2312 &value_length
, &flags
, &rc
)))
2317 test_true(count
== 4);
2320 return TEST_SUCCESS
;
2323 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2325 memcached_return_t rc
;
2326 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2327 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2328 char return_key
[MEMCACHED_MAX_KEY
];
2329 size_t return_key_length
;
2331 size_t value_length
;
2335 char insert_data
[VALUE_SIZE_BUG5
];
2337 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2338 insert_data
[x
]= (signed char)rand();
2340 memcached_flush(memc
, 0);
2341 value
= memcached_get(memc
, keys
[0], key_length
[0],
2342 &value_length
, &flags
, &rc
);
2343 test_true(value
== NULL
);
2344 test_true(rc
== MEMCACHED_NOTFOUND
);
2345 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2346 test_true(rc
== MEMCACHED_SUCCESS
);
2349 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2350 &value_length
, &flags
, &rc
)))
2352 test_true(count
== 0);
2353 test_true(rc
== MEMCACHED_END
);
2355 for (x
= 0; x
< 4; x
++)
2357 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2358 insert_data
, VALUE_SIZE_BUG5
,
2359 (time_t)0, (uint32_t)0);
2360 test_true(rc
== MEMCACHED_SUCCESS
);
2363 for (x
= 0; x
< 2; x
++)
2365 value
= memcached_get(memc
, keys
[0], key_length
[0],
2366 &value_length
, &flags
, &rc
);
2370 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2371 test_true(rc
== MEMCACHED_SUCCESS
);
2373 /* We test for purge of partial complete fetches */
2374 for (count
= 3; count
; count
--)
2376 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2377 &value_length
, &flags
, &rc
);
2378 test_true(rc
== MEMCACHED_SUCCESS
);
2379 test_true(!(memcmp(value
, insert_data
, value_length
)));
2380 test_true(value_length
);
2385 return TEST_SUCCESS
;
2388 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2390 memcached_return_t rc
;
2392 memcached_st
*memc_clone
;
2394 memcached_server_st
*servers
;
2395 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";
2397 servers
= memcached_servers_parse(server_list
);
2400 mine
= memcached_create(NULL
);
2401 rc
= memcached_server_push(mine
, servers
);
2402 test_true(rc
== MEMCACHED_SUCCESS
);
2403 memcached_server_list_free(servers
);
2406 memc_clone
= memcached_clone(NULL
, mine
);
2408 memcached_quit(mine
);
2409 memcached_quit(memc_clone
);
2412 memcached_free(mine
);
2413 memcached_free(memc_clone
);
2415 return TEST_SUCCESS
;
2418 /* Test flag store/retrieve */
2419 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2421 memcached_return_t rc
;
2422 const char *keys
= "036790384900";
2423 size_t key_length
= strlen(keys
);
2424 char return_key
[MEMCACHED_MAX_KEY
];
2425 size_t return_key_length
;
2427 size_t value_length
;
2430 char insert_data
[VALUE_SIZE_BUG5
];
2432 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2433 insert_data
[x
]= (signed char)rand();
2435 memcached_flush(memc
, 0);
2438 rc
= memcached_set(memc
, keys
, key_length
,
2439 insert_data
, VALUE_SIZE_BUG5
,
2441 test_true(rc
== MEMCACHED_SUCCESS
);
2444 value
= memcached_get(memc
, keys
, key_length
,
2445 &value_length
, &flags
, &rc
);
2446 test_true(flags
== 245);
2450 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2453 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2454 &value_length
, &flags
, &rc
);
2455 test_true(flags
== 245);
2460 return TEST_SUCCESS
;
2463 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2465 memcached_return_t rc
;
2466 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2467 size_t key_length
[3];
2472 char return_key
[MEMCACHED_MAX_KEY
];
2473 size_t return_key_length
;
2475 size_t return_value_length
;
2478 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2479 key_length
[1]= strlen("fudge&*@#");
2480 key_length
[2]= strlen("for^#@&$not");
2483 for (x
= 0; x
< 3; x
++)
2485 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2486 keys
[x
], key_length
[x
],
2487 (time_t)50, (uint32_t)9);
2488 test_true(rc
== MEMCACHED_SUCCESS
);
2491 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2492 test_true(rc
== MEMCACHED_SUCCESS
);
2494 /* We need to empty the server before continueing test */
2495 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2496 &return_value_length
, &flags
, &rc
)) != NULL
)
2498 test_true(return_value
);
2502 test_true(count
== 3);
2504 return TEST_SUCCESS
;
2507 /* We are testing with aggressive timeout to get failures */
2508 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2510 const char *key
= "foo";
2512 size_t value_length
= 512;
2515 memcached_return_t rc
;
2516 unsigned int set
= 1;
2517 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2520 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2521 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2523 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2526 value
= (char*)malloc(value_length
* sizeof(char));
2528 for (x
= 0; x
< value_length
; x
++)
2529 value
[x
]= (char) (x
% 127);
2531 for (x
= 1; x
<= 100000; ++x
)
2533 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2535 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2536 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2538 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2543 memcached_free(mclone
);
2545 return TEST_SUCCESS
;
2549 We are looking failures in the async protocol
2551 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2553 const char *key
= "foo";
2555 size_t value_length
= 512;
2558 memcached_return_t rc
;
2559 unsigned int set
= 1;
2561 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2563 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2564 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2566 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2569 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2571 test_true(timeout
== -1);
2573 value
= (char*)malloc(value_length
* sizeof(char));
2575 for (x
= 0; x
< value_length
; x
++)
2576 value
[x
]= (char) (x
% 127);
2578 for (x
= 1; x
<= 100000; ++x
)
2580 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2584 memcached_free(mclone
);
2586 return TEST_SUCCESS
;
2590 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2592 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2594 memcached_return_t rc
;
2596 size_t value_length
;
2598 uint64_t number_value
;
2600 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2601 &value_length
, &flags
, &rc
);
2602 test_true(value
== NULL
);
2603 test_true(rc
== MEMCACHED_NOTFOUND
);
2605 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2608 test_true(value
== NULL
);
2609 /* The binary protocol will set the key if it doesn't exist */
2610 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2612 test_true(rc
== MEMCACHED_SUCCESS
);
2616 test_true(rc
== MEMCACHED_NOTFOUND
);
2619 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2621 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2622 &value_length
, &flags
, &rc
);
2624 test_true(rc
== MEMCACHED_SUCCESS
);
2627 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2629 test_true(number_value
== 2);
2630 test_true(rc
== MEMCACHED_SUCCESS
);
2632 return TEST_SUCCESS
;
2636 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2637 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2639 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2641 char key
[] = "key34567890";
2643 memcached_return_t rc
;
2644 size_t overflowSize
;
2646 char commandFirst
[]= "set key34567890 0 0 ";
2647 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2648 size_t commandLength
;
2651 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2653 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2655 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2657 overflow
= malloc(testSize
);
2658 test_true(overflow
!= NULL
);
2660 memset(overflow
, 'x', testSize
);
2661 rc
= memcached_set(memc
, key
, strlen(key
),
2662 overflow
, testSize
, 0, 0);
2663 test_true(rc
== MEMCACHED_SUCCESS
);
2667 return TEST_SUCCESS
;
2672 Test values of many different sizes
2673 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2674 set key34567890 0 0 8169 \r\n
2675 is sent followed by buffer of size 8169, followed by 8169
2677 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2680 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2681 memcached_return_t rc
;
2682 const char *key
= "foo";
2684 size_t value_length
= 18000;
2686 size_t string_length
;
2689 size_t current_length
;
2691 value
= (char*)malloc(value_length
);
2694 for (x
= 0; x
< value_length
; x
++)
2695 value
[x
] = (char) (x
% 127);
2697 for (current_length
= 0; current_length
< value_length
; current_length
++)
2699 rc
= memcached_set(memc
, key
, strlen(key
),
2700 value
, current_length
,
2701 (time_t)0, (uint32_t)0);
2702 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2704 string
= memcached_get(memc
, key
, strlen(key
),
2705 &string_length
, &flags
, &rc
);
2707 test_true(rc
== MEMCACHED_SUCCESS
);
2708 test_true(string_length
== current_length
);
2709 test_true(!memcmp(string
, value
, string_length
));
2716 return TEST_SUCCESS
;
2720 Look for zero length value problems
2722 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2725 memcached_return_t rc
;
2726 const char *key
= "mykey";
2731 for (x
= 0; x
< 2; x
++)
2733 rc
= memcached_set(memc
, key
, strlen(key
),
2735 (time_t)0, (uint32_t)0);
2737 test_true(rc
== MEMCACHED_SUCCESS
);
2739 value
= memcached_get(memc
, key
, strlen(key
),
2740 &length
, &flags
, &rc
);
2742 test_true(rc
== MEMCACHED_SUCCESS
);
2743 test_true(value
== NULL
);
2744 test_true(length
== 0);
2745 test_true(flags
== 0);
2747 value
= memcached_get(memc
, key
, strlen(key
),
2748 &length
, &flags
, &rc
);
2750 test_true(rc
== MEMCACHED_SUCCESS
);
2751 test_true(value
== NULL
);
2752 test_true(length
== 0);
2753 test_true(flags
== 0);
2756 return TEST_SUCCESS
;
2759 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2760 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2762 memcached_return_t rc
;
2763 const char *key
= "mykey";
2768 rc
= memcached_set(memc
, key
, strlen(key
),
2770 (time_t)0, UINT32_MAX
);
2772 test_true(rc
== MEMCACHED_SUCCESS
);
2774 value
= memcached_get(memc
, key
, strlen(key
),
2775 &length
, &flags
, &rc
);
2777 test_true(rc
== MEMCACHED_SUCCESS
);
2778 test_true(value
== NULL
);
2779 test_true(length
== 0);
2780 test_true(flags
== UINT32_MAX
);
2782 return TEST_SUCCESS
;
2786 /* Check the validity of chinese key*/
2787 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2789 memcached_return_t rc
;
2790 const char *key
= "豆瓣";
2791 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2796 rc
= memcached_set(memc
, key
, strlen(key
),
2797 value
, strlen(value
),
2800 test_true(rc
== MEMCACHED_SUCCESS
);
2802 value2
= memcached_get(memc
, key
, strlen(key
),
2803 &length
, &flags
, &rc
);
2805 test_true(length
==strlen(value
));
2806 test_true(rc
== MEMCACHED_SUCCESS
);
2807 test_true(memcmp(value
, value2
, length
)==0);
2810 return TEST_SUCCESS
;
2818 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2821 const memcached_server_st
*server
;
2822 memcached_return_t res
;
2826 memc
= memcached_create(NULL
);
2827 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2828 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2830 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2832 memcached_free(memc
);
2834 return TEST_SUCCESS
;
2837 /* CAS test from Andei */
2838 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2840 memcached_return_t status
;
2841 memcached_result_st
*result
, result_obj
;
2842 const char *key
= "abc";
2843 size_t key_len
= strlen("abc");
2844 const char *value
= "foobar";
2845 size_t value_len
= strlen(value
);
2847 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2849 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2850 test_true(status
== MEMCACHED_SUCCESS
);
2852 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2853 test_true(status
== MEMCACHED_SUCCESS
);
2855 result
= memcached_result_create(memc
, &result_obj
);
2858 memcached_result_create(memc
, &result_obj
);
2859 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2862 test_true(status
== MEMCACHED_SUCCESS
);
2864 memcached_result_free(result
);
2866 return TEST_SUCCESS
;
2869 #include "ketama_test_cases.h"
2870 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2872 memcached_return_t rc
;
2875 memcached_server_st
*server_pool
;
2880 memc
= memcached_create(NULL
);
2883 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2884 test_true(rc
== MEMCACHED_SUCCESS
);
2886 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2887 test_true(value
== 1);
2889 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2890 test_true(rc
== MEMCACHED_SUCCESS
);
2892 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2893 test_true(value
== MEMCACHED_HASH_MD5
);
2895 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");
2896 memcached_server_push(memc
, server_pool
);
2898 /* verify that the server list was parsed okay. */
2899 test_true(memcached_server_count(memc
) == 8);
2900 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2901 test_true(server_pool
[0].port
== 11211);
2902 test_true(server_pool
[0].weight
== 600);
2903 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2904 test_true(server_pool
[2].port
== 11211);
2905 test_true(server_pool
[2].weight
== 200);
2906 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2907 test_true(server_pool
[7].port
== 11211);
2908 test_true(server_pool
[7].weight
== 100);
2910 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2911 * us test the boundary wraparound.
2913 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2915 /* verify the standard ketama set. */
2916 for (x
= 0; x
< 99; x
++)
2918 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2920 memcached_server_instance_st instance
=
2921 memcached_server_instance_by_position(memc
, server_idx
);
2923 const char *hostname
= memcached_server_name(instance
);
2924 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2927 memcached_server_list_free(server_pool
);
2928 memcached_free(memc
);
2930 return TEST_SUCCESS
;
2933 /* Large mget() of missing keys with binary proto
2935 * If many binary quiet commands (such as getq's in an mget) fill the output
2936 * buffer and the server chooses not to respond, memcached_flush hangs. See
2937 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2940 /* sighandler_t function that always asserts false */
2941 static void fail(int unused
__attribute__((unused
)))
2947 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2949 memcached_return_t rc
;
2952 size_t* key_lengths
;
2953 void (*oldalarm
)(int);
2954 memcached_st
*memc_clone
;
2956 memc_clone
= memcached_clone(NULL
, memc
);
2957 test_true(memc_clone
);
2959 /* only binproto uses getq for mget */
2960 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2962 /* empty the cache to ensure misses (hence non-responses) */
2963 rc
= memcached_flush(memc_clone
, 0);
2964 test_true(rc
== MEMCACHED_SUCCESS
);
2966 key_lengths
= calloc(key_count
, sizeof(size_t));
2967 keys
= calloc(key_count
, sizeof(char *));
2969 for (x
= 0; x
< key_count
; x
++)
2973 snprintf(buffer
, 30, "%u", x
);
2974 keys
[x
]= strdup(buffer
);
2975 key_lengths
[x
]= strlen(keys
[x
]);
2978 oldalarm
= signal(SIGALRM
, fail
);
2981 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
2982 test_true(rc
== MEMCACHED_SUCCESS
);
2985 signal(SIGALRM
, oldalarm
);
2987 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2989 for (x
= 0; x
< key_count
; x
++)
2994 memcached_free(memc_clone
);
2996 return TEST_SUCCESS
;
2999 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3001 test_return_t test_rc
;
3002 test_rc
= pre_binary(memc
);
3004 if (test_rc
!= TEST_SUCCESS
)
3009 /* should work as of r580 */
3010 rc
= _user_supplied_bug21(memc
, 10);
3011 test_true(rc
== TEST_SUCCESS
);
3013 /* should fail as of r580 */
3014 rc
= _user_supplied_bug21(memc
, 1000);
3015 test_true(rc
== TEST_SUCCESS
);
3017 return TEST_SUCCESS
;
3020 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3023 memcached_server_instance_st instance
;
3025 memcached_return_t rc
;
3026 memcached_st
*memc
= memcached_create(NULL
);
3029 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3030 test_true(rc
== MEMCACHED_SUCCESS
);
3032 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3033 test_true(value
== 1);
3035 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3036 test_true(rc
== MEMCACHED_SUCCESS
);
3038 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3039 test_true(value
== MEMCACHED_HASH_MD5
);
3041 /* server should be removed when in delay */
3042 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3043 test_true(rc
== MEMCACHED_SUCCESS
);
3045 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3046 test_true(value
== 1);
3048 memcached_server_st
*server_pool
;
3049 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");
3050 memcached_server_push(memc
, server_pool
);
3052 /* verify that the server list was parsed okay. */
3053 test_true(memcached_server_count(memc
) == 8);
3054 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3055 test_true(server_pool
[0].port
== 11211);
3056 test_true(server_pool
[0].weight
== 600);
3057 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3058 test_true(server_pool
[2].port
== 11211);
3059 test_true(server_pool
[2].weight
== 200);
3060 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3061 test_true(server_pool
[7].port
== 11211);
3062 test_true(server_pool
[7].weight
== 100);
3064 instance
= memcached_server_instance_by_position(memc
, 2);
3065 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3066 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3069 This would not work if there were only two hosts.
3071 for (size_t x
= 0; x
< 99; x
++)
3073 memcached_autoeject(memc
);
3074 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3075 test_true(server_idx
!= 2);
3078 /* and re-added when it's back. */
3079 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3080 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3081 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3082 memc
->distribution
);
3083 for (size_t x
= 0; x
< 99; x
++)
3085 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3086 // We re-use instance from above.
3088 memcached_server_instance_by_position(memc
, server_idx
);
3089 const char *hostname
= memcached_server_name(instance
);
3090 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3093 memcached_server_list_free(server_pool
);
3094 memcached_free(memc
);
3096 return TEST_SUCCESS
;
3099 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3103 memcached_return_t rc
;
3104 memcached_st
*memc
= memcached_create(NULL
);
3108 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3109 test_true(rc
== MEMCACHED_SUCCESS
);
3111 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3112 test_true(value
== 1);
3114 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3115 test_true(rc
== MEMCACHED_SUCCESS
);
3117 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3118 test_true(value
== MEMCACHED_HASH_MD5
);
3121 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3123 memcached_server_st
*server_pool
;
3124 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");
3125 memcached_server_push(memc
, server_pool
);
3127 // @todo this needs to be refactored to actually test something.
3130 if ((fp
= fopen("ketama_keys.txt", "w")))
3134 printf("cannot write to file ketama_keys.txt");
3135 return TEST_FAILURE
;
3138 for (int x
= 0; x
< 10000; x
++)
3141 sprintf(key
, "%d", x
);
3143 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3144 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3145 in_port_t port
= memc
->hosts
[server_idx
].port
;
3146 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3147 memcached_server_instance_st instance
=
3148 memcached_server_instance_by_position(memc
, host_index
);
3152 memcached_server_list_free(server_pool
);
3153 memcached_free(memc
);
3155 return TEST_SUCCESS
;
3159 static test_return_t
result_static(memcached_st
*memc
)
3161 memcached_result_st result
;
3162 memcached_result_st
*result_ptr
;
3164 result_ptr
= memcached_result_create(memc
, &result
);
3165 test_true(result
.options
.is_allocated
== false);
3166 test_true(memcached_is_initialized(&result
) == true);
3167 test_true(result_ptr
);
3168 test_true(result_ptr
== &result
);
3170 memcached_result_free(&result
);
3172 test_true(result
.options
.is_allocated
== false);
3173 test_true(memcached_is_initialized(&result
) == false);
3175 return TEST_SUCCESS
;
3178 static test_return_t
result_alloc(memcached_st
*memc
)
3180 memcached_result_st
*result_ptr
;
3182 result_ptr
= memcached_result_create(memc
, NULL
);
3183 test_true(result_ptr
);
3184 test_true(result_ptr
->options
.is_allocated
== true);
3185 test_true(memcached_is_initialized(result_ptr
) == true);
3186 memcached_result_free(result_ptr
);
3188 return TEST_SUCCESS
;
3191 static test_return_t
string_static_null(memcached_st
*memc
)
3193 memcached_string_st string
;
3194 memcached_string_st
*string_ptr
;
3196 string_ptr
= memcached_string_create(memc
, &string
, 0);
3197 test_true(string
.options
.is_initialized
== true);
3198 test_true(string_ptr
);
3200 /* The following two better be the same! */
3201 test_true(memcached_is_allocated(string_ptr
) == false);
3202 test_true(memcached_is_allocated(&string
) == false);
3203 test_true(&string
== string_ptr
);
3205 test_true(string
.options
.is_initialized
== true);
3206 test_true(memcached_is_initialized(&string
) == true);
3207 memcached_string_free(&string
);
3208 test_true(memcached_is_initialized(&string
) == false);
3210 return TEST_SUCCESS
;
3213 static test_return_t
string_alloc_null(memcached_st
*memc
)
3215 memcached_string_st
*string
;
3217 string
= memcached_string_create(memc
, NULL
, 0);
3219 test_true(memcached_is_allocated(string
) == true);
3220 test_true(memcached_is_initialized(string
) == true);
3221 memcached_string_free(string
);
3223 return TEST_SUCCESS
;
3226 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3228 memcached_string_st
*string
;
3230 string
= memcached_string_create(memc
, NULL
, 1024);
3232 test_true(memcached_is_allocated(string
) == true);
3233 test_true(memcached_is_initialized(string
) == true);
3234 memcached_string_free(string
);
3236 return TEST_SUCCESS
;
3239 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3241 memcached_string_st
*string
;
3243 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3244 test_true(string
== NULL
);
3246 return TEST_SUCCESS
;
3249 static test_return_t
string_alloc_append(memcached_st
*memc
)
3252 char buffer
[SMALL_STRING_LEN
];
3253 memcached_string_st
*string
;
3255 /* Ring the bell! */
3256 memset(buffer
, 6, SMALL_STRING_LEN
);
3258 string
= memcached_string_create(memc
, NULL
, 100);
3260 test_true(memcached_is_allocated(string
) == true);
3261 test_true(memcached_is_initialized(string
) == true);
3263 for (x
= 0; x
< 1024; x
++)
3265 memcached_return_t rc
;
3266 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3267 test_true(rc
== MEMCACHED_SUCCESS
);
3269 test_true(memcached_is_allocated(string
) == true);
3270 memcached_string_free(string
);
3272 return TEST_SUCCESS
;
3275 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3277 memcached_return_t rc
;
3279 char buffer
[SMALL_STRING_LEN
];
3280 memcached_string_st
*string
;
3282 /* Ring the bell! */
3283 memset(buffer
, 6, SMALL_STRING_LEN
);
3285 string
= memcached_string_create(memc
, NULL
, 100);
3287 test_true(memcached_is_allocated(string
) == true);
3288 test_true(memcached_is_initialized(string
) == true);
3290 for (x
= 0; x
< 1024; x
++)
3292 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3293 test_true(rc
== MEMCACHED_SUCCESS
);
3295 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3296 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3297 test_true(memcached_is_allocated(string
) == true);
3298 memcached_string_free(string
);
3300 return TEST_SUCCESS
;
3303 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3305 pairs_free(global_pairs
);
3307 return TEST_SUCCESS
;
3310 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3312 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3313 global_count
= GLOBAL_COUNT
;
3315 for (size_t x
= 0; x
< global_count
; x
++)
3317 global_keys
[x
]= global_pairs
[x
].key
;
3318 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3321 return TEST_SUCCESS
;
3324 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3326 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3327 global_count
= GLOBAL2_COUNT
;
3329 for (size_t x
= 0; x
< global_count
; x
++)
3331 global_keys
[x
]= global_pairs
[x
].key
;
3332 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3335 return TEST_SUCCESS
;
3338 static test_return_t
generate_data(memcached_st
*memc
)
3340 execute_set(memc
, global_pairs
, global_count
);
3342 return TEST_SUCCESS
;
3345 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3347 memcached_stat_st
*stat_p
;
3348 memcached_return_t rc
;
3349 uint32_t host_index
= 0;
3350 execute_set(memc
, global_pairs
, global_count
);
3352 //TODO: hosts used size stats
3353 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3356 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3358 /* This test was changes so that "make test" would work properlly */
3360 memcached_server_instance_st instance
=
3361 memcached_server_instance_by_position(memc
, host_index
);
3363 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3365 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3368 memcached_stat_free(NULL
, stat_p
);
3370 return TEST_SUCCESS
;
3372 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3377 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3378 generate_data(memc
);
3380 return TEST_SUCCESS
;
3383 static test_return_t
get_read_count(memcached_st
*memc
)
3385 memcached_return_t rc
;
3386 memcached_st
*memc_clone
;
3388 memc_clone
= memcached_clone(NULL
, memc
);
3389 test_true(memc_clone
);
3391 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3395 size_t return_value_length
;
3399 for (size_t x
= count
= 0; x
< global_count
; x
++)
3401 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3402 &return_value_length
, &flags
, &rc
);
3403 if (rc
== MEMCACHED_SUCCESS
)
3412 memcached_free(memc_clone
);
3414 return TEST_SUCCESS
;
3417 static test_return_t
get_read(memcached_st
*memc
)
3419 memcached_return_t rc
;
3423 size_t return_value_length
;
3426 for (size_t x
= 0; x
< global_count
; x
++)
3428 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3429 &return_value_length
, &flags
, &rc
);
3431 test_true(return_value);
3432 test_true(rc == MEMCACHED_SUCCESS);
3434 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3439 return TEST_SUCCESS
;
3442 static test_return_t
mget_read(memcached_st
*memc
)
3444 memcached_return_t rc
;
3446 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3447 test_true(rc
== MEMCACHED_SUCCESS
);
3448 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3450 return TEST_SUCCESS
;
3453 static test_return_t
mget_read_result(memcached_st
*memc
)
3455 memcached_return_t rc
;
3457 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3458 test_true(rc
== MEMCACHED_SUCCESS
);
3459 /* Turn this into a help function */
3461 memcached_result_st results_obj
;
3462 memcached_result_st
*results
;
3464 results
= memcached_result_create(memc
, &results_obj
);
3466 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3469 test_true(rc
== MEMCACHED_SUCCESS
);
3472 memcached_result_free(&results_obj
);
3475 return TEST_SUCCESS
;
3478 static test_return_t
mget_read_function(memcached_st
*memc
)
3480 memcached_return_t rc
;
3482 memcached_execute_fn callbacks
[1];
3484 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3485 test_true(rc
== MEMCACHED_SUCCESS
);
3487 callbacks
[0]= &callback_counter
;
3489 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3491 return TEST_SUCCESS
;
3494 static test_return_t
delete_generate(memcached_st
*memc
)
3496 for (size_t x
= 0; x
< global_count
; x
++)
3498 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3501 return TEST_SUCCESS
;
3504 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3509 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3511 for (size_t x
= 0; x
< global_count
; x
++)
3513 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3516 return TEST_SUCCESS
;
3519 static test_return_t
add_host_test1(memcached_st
*memc
)
3521 memcached_return_t rc
;
3522 char servername
[]= "0.example.com";
3523 memcached_server_st
*servers
;
3525 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3527 test_true(1 == memcached_server_list_count(servers
));
3529 for (size_t x
= 2; x
< 20; x
++)
3531 char buffer
[SMALL_STRING_LEN
];
3533 snprintf(buffer
, SMALL_STRING_LEN
, "%zu.example.com", 400+x
);
3534 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3536 test_true(rc
== MEMCACHED_SUCCESS
);
3537 test_true(x
== memcached_server_list_count(servers
));
3540 rc
= memcached_server_push(memc
, servers
);
3541 test_true(rc
== MEMCACHED_SUCCESS
);
3542 rc
= memcached_server_push(memc
, servers
);
3543 test_true(rc
== MEMCACHED_SUCCESS
);
3545 memcached_server_list_free(servers
);
3547 return TEST_SUCCESS
;
3550 static test_return_t
pre_nonblock(memcached_st
*memc
)
3552 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3554 return TEST_SUCCESS
;
3557 static test_return_t
pre_cork(memcached_st
*memc
)
3559 memcached_return_t rc
;
3562 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3565 return TEST_SKIPPED
;
3568 if (rc
== MEMCACHED_SUCCESS
)
3569 return TEST_SUCCESS
;
3571 return TEST_SKIPPED
;
3574 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3581 return TEST_SKIPPED
;
3584 if (rc
!= TEST_SUCCESS
)
3587 return pre_nonblock(memc
);
3590 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3592 memcached_return_t rc
= MEMCACHED_FAILURE
;
3593 memcached_st
*memc_clone
;
3595 memc_clone
= memcached_clone(NULL
, memc
);
3596 test_true(memc_clone
);
3597 // The memcached_version needs to be done on a clone, because the server
3598 // will not toggle protocol on an connection.
3599 memcached_version(memc_clone
);
3601 if (libmemcached_util_version_check(memc_clone
, 1, 3, 0))
3603 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3604 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3605 test_true(rc
== MEMCACHED_SUCCESS
);
3606 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3610 return TEST_SKIPPED
;
3613 memcached_free(memc_clone
);
3615 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3618 static test_return_t
pre_murmur(memcached_st
*memc
)
3620 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3622 return TEST_SUCCESS
;
3625 static test_return_t
pre_jenkins(memcached_st
*memc
)
3627 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3629 return TEST_SUCCESS
;
3633 static test_return_t
pre_md5(memcached_st
*memc
)
3635 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3637 return TEST_SUCCESS
;
3640 static test_return_t
pre_crc(memcached_st
*memc
)
3642 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3644 return TEST_SUCCESS
;
3647 static test_return_t
pre_hsieh(memcached_st
*memc
)
3649 #ifdef HAVE_HSIEH_HASH
3650 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3651 return TEST_SUCCESS
;
3654 return TEST_SKIPPED
;
3658 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3660 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3662 return TEST_SUCCESS
;
3665 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3667 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3669 return TEST_SUCCESS
;
3672 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3674 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3676 return TEST_SUCCESS
;
3679 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3681 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3683 return TEST_SUCCESS
;
3686 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3688 memcached_return_t rc
;
3691 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3692 test_true(rc
== MEMCACHED_SUCCESS
);
3694 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3695 test_true(value
== 1);
3697 return TEST_SUCCESS
;
3700 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3702 memcached_return_t rc
;
3705 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3706 test_true(rc
== MEMCACHED_SUCCESS
);
3708 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3709 test_true(value
== 1);
3711 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3712 test_true(rc
== MEMCACHED_SUCCESS
);
3714 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3715 test_true(value
== MEMCACHED_HASH_MD5
);
3717 return TEST_SUCCESS
;
3721 @note This should be testing to see if the server really supports the binary protocol.
3723 static test_return_t
pre_binary(memcached_st
*memc
)
3725 memcached_return_t rc
= MEMCACHED_FAILURE
;
3727 if (libmemcached_util_version_check(memc
, 1, 3, 0))
3729 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3730 test_true(rc
== MEMCACHED_SUCCESS
);
3731 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3734 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3737 static test_return_t
pre_sasl(memcached_st
*memc
)
3739 memcached_return_t rc
= MEMCACHED_FAILURE
;
3741 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3742 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3743 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3744 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3746 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3748 memcached_server_st
*servers
= memcached_servers_parse(server
);
3749 test_true(servers
!= NULL
);
3750 memcached_servers_reset(memc
);
3751 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3752 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3753 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3754 test_true(rc
== MEMCACHED_SUCCESS
);
3760 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3763 static test_return_t
pre_replication(memcached_st
*memc
)
3765 test_return_t test_rc
;
3766 test_rc
= pre_binary(memc
);
3768 if (test_rc
!= TEST_SUCCESS
)
3772 * Make sure that we store the item on all servers
3773 * (master + replicas == number of servers)
3775 memcached_return_t rc
;
3776 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3777 memcached_server_count(memc
) - 1);
3778 test_true(rc
== MEMCACHED_SUCCESS
);
3779 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3781 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3785 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3789 rc
= pre_replication(memc
);
3790 if (rc
!= TEST_SUCCESS
)
3793 rc
= pre_nonblock(memc
);
3799 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3802 #ifdef HARD_MALLOC_TESTS
3803 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3811 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3814 #ifdef HARD_MALLOC_TESTS
3815 void *ret
= malloc(size
+ 8);
3818 ret
= (void*)((caddr_t
)ret
+ 8);
3821 void *ret
= malloc(size
);
3826 memset(ret
, 0xff, size
);
3833 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3836 #ifdef HARD_MALLOC_TESTS
3837 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3838 void *nmem
= realloc(real_ptr
, size
+ 8);
3843 ret
= (void*)((caddr_t
)nmem
+ 8);
3848 return realloc(mem
, size
);
3853 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3856 #ifdef HARD_MALLOC_TESTS
3857 void *mem
= my_malloc(ptr
, nelem
* size
);
3860 memset(mem
, 0, nelem
* size
);
3865 return calloc(nelem
, size
);
3870 static test_return_t
set_prefix(memcached_st
*memc
)
3872 memcached_return_t rc
;
3873 const char *key
= "mine";
3876 /* Make sure be default none exists */
3877 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3878 test_true(rc
== MEMCACHED_FAILURE
);
3880 /* Test a clean set */
3881 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3882 test_true(rc
== MEMCACHED_SUCCESS
);
3884 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3885 test_true(memcmp(value
, key
, 4) == 0);
3886 test_true(rc
== MEMCACHED_SUCCESS
);
3888 /* Test that we can turn it off */
3889 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3890 test_true(rc
== MEMCACHED_SUCCESS
);
3892 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3893 test_true(rc
== MEMCACHED_FAILURE
);
3895 /* Now setup for main test */
3896 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3897 test_true(rc
== MEMCACHED_SUCCESS
);
3899 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3900 test_true(rc
== MEMCACHED_SUCCESS
);
3901 test_true(memcmp(value
, key
, 4) == 0);
3903 /* Set to Zero, and then Set to something too large */
3906 memset(long_key
, 0, 255);
3908 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3909 test_true(rc
== MEMCACHED_SUCCESS
);
3911 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3912 test_true(rc
== MEMCACHED_FAILURE
);
3913 test_true(value
== NULL
);
3915 /* Test a long key for failure */
3916 /* TODO, extend test to determine based on setting, what result should be */
3917 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3918 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3919 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3920 test_true(rc
== MEMCACHED_SUCCESS
);
3922 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3923 strcpy(long_key
, "This is more then the allotted number of characters");
3924 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3925 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3927 /* Test for a bad prefix, but with a short key */
3928 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3929 test_true(rc
== MEMCACHED_SUCCESS
);
3931 strcpy(long_key
, "dog cat");
3932 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3933 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3936 return TEST_SUCCESS
;
3940 #ifdef MEMCACHED_ENABLE_DEPRECATED
3941 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3943 void *test_ptr
= NULL
;
3946 memcached_malloc_fn malloc_cb
=
3947 (memcached_malloc_fn
)my_malloc
;
3948 cb_ptr
= *(void **)&malloc_cb
;
3949 memcached_return_t rc
;
3951 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3952 test_true(rc
== MEMCACHED_SUCCESS
);
3953 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3954 test_true(rc
== MEMCACHED_SUCCESS
);
3955 test_true(test_ptr
== cb_ptr
);
3959 memcached_realloc_fn realloc_cb
=
3960 (memcached_realloc_fn
)my_realloc
;
3961 cb_ptr
= *(void **)&realloc_cb
;
3962 memcached_return_t rc
;
3964 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3965 test_true(rc
== MEMCACHED_SUCCESS
);
3966 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3967 test_true(rc
== MEMCACHED_SUCCESS
);
3968 test_true(test_ptr
== cb_ptr
);
3972 memcached_free_fn free_cb
=
3973 (memcached_free_fn
)my_free
;
3974 cb_ptr
= *(void **)&free_cb
;
3975 memcached_return_t rc
;
3977 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3978 test_true(rc
== MEMCACHED_SUCCESS
);
3979 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3980 test_true(rc
== MEMCACHED_SUCCESS
);
3981 test_true(test_ptr
== cb_ptr
);
3984 return TEST_SUCCESS
;
3989 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3991 memcached_return_t rc
;
3992 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3993 my_realloc
, my_calloc
, NULL
);
3994 test_true(rc
== MEMCACHED_FAILURE
);
3996 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3997 my_realloc
, my_calloc
, NULL
);
3999 memcached_malloc_fn mem_malloc
;
4000 memcached_free_fn mem_free
;
4001 memcached_realloc_fn mem_realloc
;
4002 memcached_calloc_fn mem_calloc
;
4003 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4004 &mem_realloc
, &mem_calloc
);
4006 test_true(mem_malloc
== my_malloc
);
4007 test_true(mem_realloc
== my_realloc
);
4008 test_true(mem_calloc
== my_calloc
);
4009 test_true(mem_free
== my_free
);
4011 return TEST_SUCCESS
;
4014 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4017 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4018 memcached_hash_t hash
;
4019 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4020 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4023 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4024 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4026 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4028 if (hash
!= MEMCACHED_HASH_CRC
)
4029 return TEST_SKIPPED
;
4031 return TEST_SUCCESS
;
4034 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4037 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4038 memcached_hash_t hash
;
4039 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4040 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4043 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4044 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4046 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4048 if (hash
!= MEMCACHED_HASH_HSIEH
)
4049 return TEST_SKIPPED
;
4052 return TEST_SUCCESS
;
4055 static test_return_t
enable_cas(memcached_st
*memc
)
4057 unsigned int set
= 1;
4059 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4061 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4063 return TEST_SUCCESS
;
4066 return TEST_SKIPPED
;
4069 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4071 memcached_version(memc
);
4073 memcached_server_instance_st instance
=
4074 memcached_server_instance_by_position(memc
, 0);
4076 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4077 || instance
->minor_version
> 2)
4079 return TEST_SUCCESS
;
4082 return TEST_SKIPPED
;
4085 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4087 memcached_return_t rc
;
4090 memcached_servers_reset(memc
);
4092 if (stat("/tmp/memcached.socket", &buf
))
4093 return TEST_SKIPPED
;
4095 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4097 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4100 static test_return_t
pre_nodelay(memcached_st
*memc
)
4102 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4103 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4105 return TEST_SUCCESS
;
4108 static test_return_t
pre_settimer(memcached_st
*memc
)
4110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4111 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4113 return TEST_SUCCESS
;
4116 static test_return_t
poll_timeout(memcached_st
*memc
)
4122 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4124 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4126 test_true(timeout
== 100);
4128 return TEST_SUCCESS
;
4131 static test_return_t
noreply_test(memcached_st
*memc
)
4133 memcached_return_t ret
;
4134 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4135 test_true(ret
== MEMCACHED_SUCCESS
);
4136 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4137 test_true(ret
== MEMCACHED_SUCCESS
);
4138 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4139 test_true(ret
== MEMCACHED_SUCCESS
);
4140 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4141 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4142 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4144 for (int count
=0; count
< 5; ++count
)
4146 for (size_t x
= 0; x
< 100; ++x
)
4149 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4153 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4156 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4159 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4162 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4165 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4171 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4175 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4176 ** API and is _ONLY_ done this way to verify that the library works the
4177 ** way it is supposed to do!!!!
4180 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4182 memcached_server_instance_st instance
=
4183 memcached_server_instance_by_position(memc
, x
);
4184 no_msg
+=(int)(instance
->cursor_active
);
4187 test_true(no_msg
== 0);
4188 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4191 ** Now validate that all items was set properly!
4193 for (size_t x
= 0; x
< 100; ++x
)
4197 size_t len
= (size_t)sprintf(key
, "%zu", x
);
4200 char* value
=memcached_get(memc
, key
, strlen(key
),
4201 &length
, &flags
, &ret
);
4202 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4205 case 0: /* FALLTHROUGH */
4206 case 1: /* FALLTHROUGH */
4208 test_true(strncmp(value
, key
, len
) == 0);
4209 test_true(len
== length
);
4212 test_true(length
== len
* 2);
4215 test_true(length
== len
* 3);
4225 /* Try setting an illegal cas value (should not return an error to
4226 * the caller (because we don't expect a return message from the server)
4228 const char* keys
[]= {"0"};
4229 size_t lengths
[]= {1};
4232 memcached_result_st results_obj
;
4233 memcached_result_st
*results
;
4234 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4235 test_true(ret
== MEMCACHED_SUCCESS
);
4237 results
= memcached_result_create(memc
, &results_obj
);
4239 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4241 test_true(ret
== MEMCACHED_SUCCESS
);
4242 uint64_t cas
= memcached_result_cas(results
);
4243 memcached_result_free(&results_obj
);
4245 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4246 test_true(ret
== MEMCACHED_SUCCESS
);
4249 * The item will have a new cas value, so try to set it again with the old
4250 * value. This should fail!
4252 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4253 test_true(ret
== MEMCACHED_SUCCESS
);
4254 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4255 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4256 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4259 return TEST_SUCCESS
;
4262 static test_return_t
analyzer_test(memcached_st
*memc
)
4264 memcached_return_t rc
;
4265 memcached_stat_st
*memc_stat
;
4266 memcached_analysis_st
*report
;
4268 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4269 test_true(rc
== MEMCACHED_SUCCESS
);
4270 test_true(memc_stat
);
4272 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4273 test_true(rc
== MEMCACHED_SUCCESS
);
4277 memcached_stat_free(NULL
, memc_stat
);
4279 return TEST_SUCCESS
;
4282 /* Count the objects */
4283 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4284 const char *key
__attribute__((unused
)),
4285 size_t key_length
__attribute__((unused
)),
4288 size_t *counter
= (size_t *)context
;
4290 *counter
= *counter
+ 1;
4292 return MEMCACHED_SUCCESS
;
4295 static test_return_t
dump_test(memcached_st
*memc
)
4297 memcached_return_t rc
;
4299 memcached_dump_fn callbacks
[1];
4300 test_return_t main_rc
;
4302 callbacks
[0]= &callback_dump_counter
;
4304 /* No support for Binary protocol yet */
4305 if (memc
->flags
.binary_protocol
)
4306 return TEST_SUCCESS
;
4308 main_rc
= set_test3(memc
);
4310 test_true (main_rc
== TEST_SUCCESS
);
4312 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4313 test_true(rc
== MEMCACHED_SUCCESS
);
4315 /* We may have more then 32 if our previous flush has not completed */
4316 test_true(counter
>= 32);
4318 return TEST_SUCCESS
;
4321 #ifdef HAVE_LIBMEMCACHEDUTIL
4322 static void* connection_release(void *arg
)
4325 memcached_pool_st
* pool
;
4330 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4334 static test_return_t
connection_pool_test(memcached_st
*memc
)
4336 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, 10);
4337 test_true(pool
!= NULL
);
4338 memcached_st
* mmc
[10];
4339 memcached_return_t rc
;
4341 for (size_t x
= 0; x
< 10; ++x
)
4343 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4344 test_true(mmc
[x
] != NULL
);
4345 test_true(rc
== MEMCACHED_SUCCESS
);
4348 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4349 test_true(rc
== MEMCACHED_SUCCESS
);
4353 memcached_pool_st
* pool
;
4355 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4356 pthread_create(&tid
, NULL
, connection_release
, &item
);
4357 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4358 test_true(rc
== MEMCACHED_SUCCESS
);
4359 pthread_join(tid
, NULL
);
4360 test_true(mmc
[9] == item
.mmc
);
4361 const char *key
= "key";
4362 size_t keylen
= strlen(key
);
4364 // verify that I can do ops with all connections
4365 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4366 test_true(rc
== MEMCACHED_SUCCESS
);
4368 for (size_t x
= 0; x
< 10; ++x
)
4370 uint64_t number_value
;
4371 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4372 test_true(rc
== MEMCACHED_SUCCESS
);
4373 test_true(number_value
== (x
+1));
4377 for (size_t x
= 0; x
< 10; ++x
)
4379 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4383 /* verify that I can set behaviors on the pool when I don't have all
4384 * of the connections in the pool. It should however be enabled
4385 * when I push the item into the pool
4387 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4388 test_true(mmc
[0] != NULL
);
4390 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4391 test_true(rc
== MEMCACHED_SUCCESS
);
4393 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4394 test_true(mmc
[1] != NULL
);
4396 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4397 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4398 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4400 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4401 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4402 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4405 test_true(memcached_pool_destroy(pool
) == memc
);
4406 return TEST_SUCCESS
;
4409 static test_return_t
util_version_test(memcached_st
*memc
)
4413 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4414 test_true(if_successful
== true);
4416 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4417 test_true(if_successful
== false);
4419 memcached_server_instance_st instance
=
4420 memcached_server_instance_by_position(memc
, 0);
4422 memcached_version(memc
);
4424 // We only use one binary when we test, so this should be just fine.
4425 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4426 test_true(if_successful
== true);
4428 if (instance
->micro_version
> 0)
4429 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
-1);
4430 else if (instance
->minor_version
> 0)
4431 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
- 1, instance
->micro_version
);
4432 else if (instance
->major_version
> 0)
4433 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
-1, instance
->minor_version
, instance
->micro_version
);
4435 test_true(if_successful
== true);
4437 if (instance
->micro_version
> 0)
4438 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
+1);
4439 else if (instance
->minor_version
> 0)
4440 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
+1, instance
->micro_version
);
4441 else if (instance
->major_version
> 0)
4442 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
+1, instance
->minor_version
, instance
->micro_version
);
4444 test_true(if_successful
== false);
4446 return TEST_SUCCESS
;
4449 static test_return_t
ping_test(memcached_st
*memc
)
4451 memcached_return_t rc
;
4452 memcached_server_instance_st instance
=
4453 memcached_server_instance_by_position(memc
, 0);
4455 // Test both the version that returns a code, and the one that does not.
4456 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4457 memcached_server_port(instance
), NULL
));
4459 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4460 memcached_server_port(instance
), &rc
));
4462 test_true(rc
== MEMCACHED_SUCCESS
);
4464 return TEST_SUCCESS
;
4468 static test_return_t
replication_set_test(memcached_st
*memc
)
4470 memcached_return_t rc
;
4471 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4472 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4474 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4475 test_true(rc
== MEMCACHED_SUCCESS
);
4478 ** We are using the quiet commands to store the replicas, so we need
4479 ** to ensure that all of them are processed before we can continue.
4480 ** In the test we go directly from storing the object to trying to
4481 ** receive the object from all of the different servers, so we
4482 ** could end up in a race condition (the memcached server hasn't yet
4483 ** processed the quiet command from the replication set when it process
4484 ** the request from the other client (created by the clone)). As a
4485 ** workaround for that we call memcached_quit to send the quit command
4486 ** to the server and wait for the response ;-) If you use the test code
4487 ** as an example for your own code, please note that you shouldn't need
4490 memcached_quit(memc
);
4493 ** "bubba" should now be stored on all of our servers. We don't have an
4494 ** easy to use API to address each individual server, so I'll just iterate
4495 ** through a bunch of "master keys" and I should most likely hit all of the
4498 for (int x
= 'a'; x
<= 'z'; ++x
)
4500 char key
[2]= { [0]= (char)x
};
4503 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4505 test_true(rc
== MEMCACHED_SUCCESS
);
4506 test_true(val
!= NULL
);
4510 memcached_free(memc_clone
);
4512 return TEST_SUCCESS
;
4515 static test_return_t
replication_get_test(memcached_st
*memc
)
4517 memcached_return_t rc
;
4520 * Don't do the following in your code. I am abusing the internal details
4521 * within the library, and this is not a supported interface.
4522 * This is to verify correct behavior in the library
4524 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4526 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4527 memcached_server_instance_st instance
=
4528 memcached_server_instance_by_position(memc_clone
, host
);
4530 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4532 for (int x
= 'a'; x
<= 'z'; ++x
)
4534 char key
[2]= { [0]= (char)x
};
4537 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4539 test_true(rc
== MEMCACHED_SUCCESS
);
4540 test_true(val
!= NULL
);
4544 memcached_free(memc_clone
);
4547 return TEST_SUCCESS
;
4550 static test_return_t
replication_mget_test(memcached_st
*memc
)
4552 memcached_return_t rc
;
4553 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4554 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4556 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4557 size_t len
[]= { 5, 4, 4, 4 };
4559 for (size_t x
= 0; x
< 4; ++x
)
4561 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4562 test_true(rc
== MEMCACHED_SUCCESS
);
4566 ** We are using the quiet commands to store the replicas, so we need
4567 ** to ensure that all of them are processed before we can continue.
4568 ** In the test we go directly from storing the object to trying to
4569 ** receive the object from all of the different servers, so we
4570 ** could end up in a race condition (the memcached server hasn't yet
4571 ** processed the quiet command from the replication set when it process
4572 ** the request from the other client (created by the clone)). As a
4573 ** workaround for that we call memcached_quit to send the quit command
4574 ** to the server and wait for the response ;-) If you use the test code
4575 ** as an example for your own code, please note that you shouldn't need
4578 memcached_quit(memc
);
4581 * Don't do the following in your code. I am abusing the internal details
4582 * within the library, and this is not a supported interface.
4583 * This is to verify correct behavior in the library
4585 memcached_result_st result_obj
;
4586 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4588 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4589 memcached_server_instance_st instance
=
4590 memcached_server_instance_by_position(new_clone
, host
);
4591 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4593 for (int x
= 'a'; x
<= 'z'; ++x
)
4595 char key
[2]= { [0]= (char)x
, [1]= 0 };
4597 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4598 test_true(rc
== MEMCACHED_SUCCESS
);
4600 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4604 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4608 test_true(hits
== 4);
4609 memcached_result_free(&result_obj
);
4612 memcached_free(new_clone
);
4615 memcached_free(memc_clone
);
4617 return TEST_SUCCESS
;
4620 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4622 memcached_result_st result_obj
;
4623 memcached_return_t rc
;
4624 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4625 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4626 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4628 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4629 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4631 for (size_t x
= 0; x
< 7; ++x
)
4633 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4634 test_true(rc
== MEMCACHED_SUCCESS
);
4637 memcached_quit(memc
);
4639 for (size_t x
= 0; x
< 7; ++x
)
4641 const char key
[2]= { [0]= (const char)x
};
4643 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4644 test_true(rc
== MEMCACHED_SUCCESS
);
4646 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4650 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4654 test_true(hits
== 7);
4655 memcached_result_free(&result_obj
);
4657 memcached_free(memc_clone
);
4658 return TEST_SUCCESS
;
4661 static test_return_t
replication_delete_test(memcached_st
*memc
)
4663 memcached_return_t rc
;
4664 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4665 /* Delete the items from all of the servers except 1 */
4666 uint64_t repl
= memcached_behavior_get(memc
,
4667 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4668 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4670 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4671 size_t len
[]= { 5, 4, 4, 4 };
4673 for (size_t x
= 0; x
< 4; ++x
)
4675 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4676 test_true(rc
== MEMCACHED_SUCCESS
);
4680 * Don't do the following in your code. I am abusing the internal details
4681 * within the library, and this is not a supported interface.
4682 * This is to verify correct behavior in the library
4684 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4685 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4687 memcached_server_instance_st instance
=
4688 memcached_server_instance_by_position(memc_clone
, x
);
4690 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4691 if (++hash
== memc_clone
->number_of_hosts
)
4695 memcached_result_st result_obj
;
4696 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4698 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4700 const char key
[2]= { [0]= (const char)x
};
4702 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4703 test_true(rc
== MEMCACHED_SUCCESS
);
4705 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4709 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4713 test_true(hits
== 4);
4714 memcached_result_free(&result_obj
);
4717 memcached_free(memc_clone
);
4719 return TEST_SUCCESS
;
4723 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4727 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4728 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4729 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4730 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4731 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4732 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4733 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4734 #ifdef HAVE_HSIEH_HASH
4735 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4737 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4738 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4739 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4741 return TEST_SUCCESS
;
4745 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4747 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4748 #ifdef HAVE_HSIEH_HASH
4749 expected_rc
= MEMCACHED_SUCCESS
;
4751 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4752 (uint64_t)MEMCACHED_HASH_HSIEH
);
4753 test_true(rc
== expected_rc
);
4755 return TEST_SUCCESS
;
4758 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4763 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4767 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4768 test_true(one_at_a_time_values
[x
] == hash_val
);
4771 return TEST_SUCCESS
;
4774 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4779 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4783 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4784 test_true(md5_values
[x
] == hash_val
);
4787 return TEST_SUCCESS
;
4790 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4795 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4799 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4800 test_true(crc_values
[x
] == hash_val
);
4803 return TEST_SUCCESS
;
4806 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4811 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4815 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4816 test_true(fnv1_64_values
[x
] == hash_val
);
4819 return TEST_SUCCESS
;
4822 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4827 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4831 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4832 test_true(fnv1a_64_values
[x
] == hash_val
);
4835 return TEST_SUCCESS
;
4838 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4844 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4848 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4849 test_true(fnv1_32_values
[x
] == hash_val
);
4852 return TEST_SUCCESS
;
4855 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4860 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4864 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4865 test_true(fnv1a_32_values
[x
] == hash_val
);
4868 return TEST_SUCCESS
;
4871 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4876 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4880 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4881 test_true(hsieh_values
[x
] == hash_val
);
4884 return TEST_SUCCESS
;
4887 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4889 #ifdef WORDS_BIGENDIAN
4890 return TEST_SKIPPED
;
4895 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4899 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4900 test_true(murmur_values
[x
] == hash_val
);
4903 return TEST_SUCCESS
;
4907 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4913 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4917 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4918 test_true(jenkins_values
[x
] == hash_val
);
4921 return TEST_SUCCESS
;
4924 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4927 return libhashkit_md5(string
, string_length
);
4930 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4933 return libhashkit_crc32(string
, string_length
);
4936 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4940 const hashkit_st
*kit
;
4942 hashkit_return_t hash_rc
;
4944 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};
4945 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};
4947 kit
= memcached_get_hashkit(memc
);
4949 hashkit_clone(&new_kit
, kit
);
4950 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4951 test_true(hash_rc
== HASHKIT_SUCCESS
);
4953 memcached_set_hashkit(memc
, &new_kit
);
4956 Verify Setting the hash.
4958 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4962 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4963 test_true(md5_values
[x
] == hash_val
);
4968 Now check memcached_st.
4970 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4974 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4975 test_true(md5_hosts
[x
] == hash_val
);
4978 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4979 test_true(hash_rc
== HASHKIT_SUCCESS
);
4981 memcached_set_hashkit(memc
, &new_kit
);
4984 Verify Setting the hash.
4986 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4990 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4991 test_true(crc_values
[x
] == hash_val
);
4994 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4998 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4999 test_true(crc_hosts
[x
] == hash_val
);
5002 return TEST_SUCCESS
;
5006 Test case adapted from John Gorman <johngorman2@gmail.com>
5008 We are testing the error condition when we connect to a server via memcached_get()
5009 but find that the server is not available.
5011 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
5014 memcached_st
*tl_memc_h
;
5015 memcached_server_st
*servers
;
5017 const char *key
= "MemcachedLives";
5020 memcached_return rc
;
5024 tl_memc_h
= memcached_create(NULL
);
5025 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5026 memcached_server_push(tl_memc_h
, servers
);
5027 memcached_server_list_free(servers
);
5029 // See if memcached is reachable.
5030 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5035 test_true(value
); // Pointer won't be zero so this is fine.
5038 test_true(len
== 0);
5039 test_true(rc
== MEMCACHED_ERRNO
);
5041 return TEST_SUCCESS
;
5045 We connect to a server which exists, but search for a key that does not exist.
5047 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5049 const char *key
= "MemcachedKeyNotEXIST";
5052 memcached_return rc
;
5055 // See if memcached is reachable.
5056 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5061 test_true(value
); // Pointer won't be zero so this is fine.
5064 test_true(len
== 0);
5065 test_true(rc
== MEMCACHED_NOTFOUND
);
5067 return TEST_SUCCESS
;
5071 Test case adapted from John Gorman <johngorman2@gmail.com>
5073 We are testing the error condition when we connect to a server via memcached_get_by_key()
5074 but find that the server is not available.
5076 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
5079 memcached_st
*tl_memc_h
;
5080 memcached_server_st
*servers
;
5082 const char *key
= "MemcachedLives";
5085 memcached_return rc
;
5089 tl_memc_h
= memcached_create(NULL
);
5090 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5091 memcached_server_push(tl_memc_h
, servers
);
5092 memcached_server_list_free(servers
);
5094 // See if memcached is reachable.
5095 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5100 test_true(value
); // Pointer won't be zero so this is fine.
5103 test_true(len
== 0);
5104 test_true(rc
== MEMCACHED_ERRNO
);
5106 return TEST_SUCCESS
;
5110 We connect to a server which exists, but search for a key that does not exist.
5112 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5114 const char *key
= "MemcachedKeyNotEXIST";
5117 memcached_return rc
;
5120 // See if memcached is reachable.
5121 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5126 test_true(value
); // Pointer won't be zero so this is fine.
5129 test_true(len
== 0);
5130 test_true(rc
== MEMCACHED_NOTFOUND
);
5132 return TEST_SUCCESS
;
5136 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5138 memcached_return_t rc
;
5141 memcached_server_st
*server_pool
;
5146 memc
= memcached_create(NULL
);
5149 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5150 test_true(rc
== MEMCACHED_SUCCESS
);
5152 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5153 test_true(value
== 1);
5155 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5156 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5159 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");
5160 memcached_server_push(memc
, server_pool
);
5162 /* verify that the server list was parsed okay. */
5163 test_true(memcached_server_count(memc
) == 8);
5164 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5165 test_true(server_pool
[0].port
== 11211);
5166 test_true(server_pool
[0].weight
== 600);
5167 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5168 test_true(server_pool
[2].port
== 11211);
5169 test_true(server_pool
[2].weight
== 200);
5170 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5171 test_true(server_pool
[7].port
== 11211);
5172 test_true(server_pool
[7].weight
== 100);
5174 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5175 * us test the boundary wraparound.
5177 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5179 /* verify the standard ketama set. */
5180 for (x
= 0; x
< 99; x
++)
5182 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5183 memcached_server_instance_st instance
=
5184 memcached_server_instance_by_position(memc
, server_idx
);
5185 const char *hostname
= memcached_server_name(instance
);
5187 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5190 memcached_server_list_free(server_pool
);
5191 memcached_free(memc
);
5193 return TEST_SUCCESS
;
5196 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5198 memcached_return_t rc
;
5201 memcached_server_st
*server_pool
;
5206 memc
= memcached_create(NULL
);
5209 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5210 test_true(rc
== MEMCACHED_SUCCESS
);
5212 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5213 test_true(value
== 1);
5215 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5216 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5218 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");
5219 memcached_server_push(memc
, server_pool
);
5221 /* verify that the server list was parsed okay. */
5222 test_true(memcached_server_count(memc
) == 8);
5223 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5224 test_true(server_pool
[0].port
== 11211);
5225 test_true(server_pool
[0].weight
== 600);
5226 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5227 test_true(server_pool
[2].port
== 11211);
5228 test_true(server_pool
[2].weight
== 200);
5229 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5230 test_true(server_pool
[7].port
== 11211);
5231 test_true(server_pool
[7].weight
== 100);
5233 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5234 * us test the boundary wraparound.
5236 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5238 /* verify the standard ketama set. */
5239 for (x
= 0; x
< 99; x
++)
5241 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5243 memcached_server_instance_st instance
=
5244 memcached_server_instance_by_position(memc
, server_idx
);
5246 const char *hostname
= memcached_server_name(instance
);
5248 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5251 memcached_server_list_free(server_pool
);
5252 memcached_free(memc
);
5254 return TEST_SUCCESS
;
5257 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5259 test_return_t test_rc
;
5260 test_rc
= pre_binary(memc
);
5262 if (test_rc
!= TEST_SUCCESS
)
5265 memcached_return_t ret
;
5266 const char *key
= "regression_bug_434484";
5267 size_t keylen
= strlen(key
);
5269 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5270 test_true(ret
== MEMCACHED_NOTSTORED
);
5272 size_t size
= 2048 * 1024;
5273 void *data
= calloc(1, size
);
5274 test_true(data
!= NULL
);
5275 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5276 test_true(ret
== MEMCACHED_E2BIG
);
5279 return TEST_SUCCESS
;
5282 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5284 test_return_t test_rc
;
5285 test_rc
= pre_binary(memc
);
5287 if (test_rc
!= TEST_SUCCESS
)
5290 memcached_return_t rc
;
5292 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5295 * I only want to hit only _one_ server so I know the number of requests I'm
5296 * sending in the pipleine to the server. Let's try to do a multiget of
5297 * 1024 (that should satisfy most users don't you think?). Future versions
5298 * will include a mget_execute function call if you need a higher number.
5300 uint32_t number_of_hosts
= memcached_server_count(memc
);
5301 memc
->number_of_hosts
= 1;
5302 const size_t max_keys
= 1024;
5303 char **keys
= calloc(max_keys
, sizeof(char*));
5304 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5306 for (size_t x
= 0; x
< max_keys
; ++x
)
5310 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5312 test_true(keys
[x
] != NULL
);
5316 * Run two times.. the first time we should have 100% cache miss,
5317 * and the second time we should have 100% cache hits
5319 for (size_t y
= 0; y
< 2; y
++)
5321 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5322 test_true(rc
== MEMCACHED_SUCCESS
);
5323 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5327 /* The first iteration should give me a 100% cache miss. verify that*/
5328 char blob
[1024]= { 0 };
5330 test_true(counter
== 0);
5332 for (size_t x
= 0; x
< max_keys
; ++x
)
5334 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5335 blob
, sizeof(blob
), 0, 0);
5336 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5341 /* Verify that we received all of the key/value pairs */
5342 test_true(counter
== max_keys
);
5346 /* Release allocated resources */
5347 for (size_t x
= 0; x
< max_keys
; ++x
)
5354 memc
->number_of_hosts
= number_of_hosts
;
5356 return TEST_SUCCESS
;
5359 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5361 memcached_return_t rc
;
5362 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5363 test_true(rc
== MEMCACHED_SUCCESS
);
5365 return regression_bug_434843(memc
);
5368 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5370 memcached_return_t rc
;
5371 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5372 test_true(rc
== MEMCACHED_SUCCESS
);
5374 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5375 test_true(rc
== MEMCACHED_SUCCESS
);
5376 test_true(bytes
!= NULL
);
5377 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5379 test_true(rc
== MEMCACHED_SUCCESS
);
5380 test_true(bytes_read
!= NULL
);
5382 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5383 "bytes_written", &rc
);
5384 test_true(rc
== MEMCACHED_SUCCESS
);
5385 test_true(bytes_written
!= NULL
);
5387 test_true(strcmp(bytes
, bytes_read
) != 0);
5388 test_true(strcmp(bytes
, bytes_written
) != 0);
5390 /* Release allocated resources */
5393 free(bytes_written
);
5394 memcached_stat_free(NULL
, memc_stat
);
5396 return TEST_SUCCESS
;
5400 * The test case isn't obvious so I should probably document why
5401 * it works the way it does. Bug 442914 was caused by a bug
5402 * in the logic in memcached_purge (it did not handle the case
5403 * where the number of bytes sent was equal to the watermark).
5404 * In this test case, create messages so that we hit that case
5405 * and then disable noreply mode and issue a new command to
5406 * verify that it isn't stuck. If we change the format for the
5407 * delete command or the watermarks, we need to update this
5410 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5412 memcached_return_t rc
;
5413 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5414 test_true(rc
== MEMCACHED_SUCCESS
);
5415 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5417 uint32_t number_of_hosts
= memcached_server_count(memc
);
5418 memc
->number_of_hosts
= 1;
5423 for (uint32_t x
= 0; x
< 250; ++x
)
5425 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5426 rc
= memcached_delete(memc
, k
, len
, 0);
5427 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5430 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5433 rc
= memcached_delete(memc
, k
, len
, 0);
5434 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5436 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5437 test_true(rc
== MEMCACHED_SUCCESS
);
5438 rc
= memcached_delete(memc
, k
, len
, 0);
5439 test_true(rc
== MEMCACHED_NOTFOUND
);
5441 memc
->number_of_hosts
= number_of_hosts
;
5443 return TEST_SUCCESS
;
5446 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5448 memcached_server_instance_st instance_one
;
5449 memcached_server_instance_st instance_two
;
5451 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != MEMCACHED_SUCCESS
)
5452 return TEST_SKIPPED
;
5454 memcached_return_t rc
;
5456 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5457 test_true(rc
== MEMCACHED_SUCCESS
);
5459 const size_t max_keys
= 100;
5460 char **keys
= calloc(max_keys
, sizeof(char*));
5461 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5463 for (size_t x
= 0; x
< max_keys
; ++x
)
5467 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5469 test_true(keys
[x
] != NULL
);
5470 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5471 test_true(rc
== MEMCACHED_SUCCESS
);
5475 ** We are using the quiet commands to store the replicas, so we need
5476 ** to ensure that all of them are processed before we can continue.
5477 ** In the test we go directly from storing the object to trying to
5478 ** receive the object from all of the different servers, so we
5479 ** could end up in a race condition (the memcached server hasn't yet
5480 ** processed the quiet command from the replication set when it process
5481 ** the request from the other client (created by the clone)). As a
5482 ** workaround for that we call memcached_quit to send the quit command
5483 ** to the server and wait for the response ;-) If you use the test code
5484 ** as an example for your own code, please note that you shouldn't need
5487 memcached_quit(memc
);
5489 /* Verify that all messages are stored, and we didn't stuff too much
5492 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5493 test_true(rc
== MEMCACHED_SUCCESS
);
5496 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5497 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5498 /* Verify that we received all of the key/value pairs */
5499 test_true(counter
== max_keys
);
5501 memcached_quit(memc
);
5503 * Don't do the following in your code. I am abusing the internal details
5504 * within the library, and this is not a supported interface.
5505 * This is to verify correct behavior in the library. Fake that two servers
5508 instance_one
= memcached_server_instance_by_position(memc
, 0);
5509 instance_two
= memcached_server_instance_by_position(memc
, 2);
5510 in_port_t port0
= instance_one
->port
;
5511 in_port_t port2
= instance_two
->port
;
5513 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5514 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5516 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5517 test_true(rc
== MEMCACHED_SUCCESS
);
5520 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5521 test_true(counter
== (unsigned int)max_keys
);
5523 /* restore the memc handle */
5524 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5525 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5527 memcached_quit(memc
);
5529 /* Remove half of the objects */
5530 for (size_t x
= 0; x
< max_keys
; ++x
)
5534 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5535 test_true(rc
== MEMCACHED_SUCCESS
);
5539 memcached_quit(memc
);
5540 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5541 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5543 /* now retry the command, this time we should have cache misses */
5544 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5545 test_true(rc
== MEMCACHED_SUCCESS
);
5548 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5549 test_true(counter
== (unsigned int)(max_keys
>> 1));
5551 /* Release allocated resources */
5552 for (size_t x
= 0; x
< max_keys
; ++x
)
5559 /* restore the memc handle */
5560 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5561 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5563 return TEST_SUCCESS
;
5566 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5568 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5569 test_true(memc_clone
!= NULL
);
5570 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5572 memcached_server_instance_st instance
=
5573 memcached_server_instance_by_position(memc_clone
, 0);
5575 if (instance
->major_version
> 1 ||
5576 (instance
->major_version
== 1 &&
5577 instance
->minor_version
> 2))
5579 /* Binary protocol doesn't support deferred delete */
5580 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5581 test_true(bin_clone
!= NULL
);
5582 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5583 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5584 memcached_free(bin_clone
);
5586 memcached_quit(memc_clone
);
5588 /* If we know the server version, deferred delete should fail
5589 * with invalid arguments */
5590 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5592 /* If we don't know the server version, we should get a protocol error */
5593 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5595 /* but there is a bug in some of the memcached servers (1.4) that treats
5596 * the counter as noreply so it doesn't send the proper error message
5598 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5600 /* And buffered mode should be disabled and we should get protocol error */
5601 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5602 rc
= memcached_delete(memc
, "foo", 3, 1);
5603 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5605 /* Same goes for noreply... */
5606 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5607 rc
= memcached_delete(memc
, "foo", 3, 1);
5608 test_true(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
);
5610 /* but a normal request should go through (and be buffered) */
5611 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5612 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5614 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5615 /* unbuffered noreply should be success */
5616 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5617 /* unbuffered with reply should be not found... */
5618 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5619 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5622 memcached_free(memc_clone
);
5623 return TEST_SUCCESS
;
5627 /* Test memcached_server_get_last_disconnect
5628 * For a working server set, shall be NULL
5629 * For a set of non existing server, shall not be NULL
5631 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5633 memcached_return_t rc
;
5634 memcached_server_instance_st disconnected_server
;
5636 /* With the working set of server */
5637 const char *key
= "marmotte";
5638 const char *value
= "milka";
5640 memcached_reset_last_disconnected_server(memc
);
5641 rc
= memcached_set(memc
, key
, strlen(key
),
5642 value
, strlen(value
),
5643 (time_t)0, (uint32_t)0);
5644 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5646 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5647 test_true(disconnected_server
== NULL
);
5649 /* With a non existing server */
5651 memcached_server_st
*servers
;
5653 const char *server_list
= "localhost:9";
5655 servers
= memcached_servers_parse(server_list
);
5657 mine
= memcached_create(NULL
);
5658 rc
= memcached_server_push(mine
, servers
);
5659 test_true(rc
== MEMCACHED_SUCCESS
);
5660 memcached_server_list_free(servers
);
5663 rc
= memcached_set(mine
, key
, strlen(key
),
5664 value
, strlen(value
),
5665 (time_t)0, (uint32_t)0);
5666 test_true(rc
!= MEMCACHED_SUCCESS
);
5668 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5669 if (disconnected_server
== NULL
)
5671 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5674 test_true(disconnected_server
!= NULL
);
5675 test_true(memcached_server_port(disconnected_server
)== 9);
5676 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5678 memcached_quit(mine
);
5679 memcached_free(mine
);
5681 return TEST_SUCCESS
;
5684 static test_return_t
test_verbosity(memcached_st
*memc
)
5686 memcached_verbosity(memc
, 3);
5688 return TEST_SUCCESS
;
5691 static test_return_t
test_server_failure(memcached_st
*memc
)
5693 memcached_st
*local_memc
;
5694 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5696 local_memc
= memcached_create(NULL
);
5698 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5699 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5701 uint32_t server_count
= memcached_server_count(local_memc
);
5703 test_true(server_count
== 1);
5705 // Disable the server
5706 instance
= memcached_server_instance_by_position(local_memc
, 0);
5707 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5709 memcached_return_t rc
;
5710 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5712 (time_t)0, (uint32_t)0);
5713 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5715 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5716 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5718 (time_t)0, (uint32_t)0);
5719 test_true(rc
== MEMCACHED_SUCCESS
);
5722 memcached_free(local_memc
);
5724 return TEST_SUCCESS
;
5727 static test_return_t
test_cull_servers(memcached_st
*memc
)
5729 uint32_t count
= memcached_server_count(memc
);
5731 // Do not do this in your code, it is not supported.
5732 memc
->servers
[1].state
.is_dead
= true;
5733 memc
->state
.is_time_for_rebuild
= true;
5735 uint32_t new_count
= memcached_server_count(memc
);
5736 test_true(count
== new_count
);
5739 test_true(count
== new_count
+ 1 );
5742 return TEST_SUCCESS
;
5746 * This test ensures that the failure counter isn't incremented during
5747 * normal termination of the memcached instance.
5749 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5751 memcached_return_t rc
;
5752 memcached_server_instance_st instance
;
5754 /* Set value to force connection to the server */
5755 const char *key
= "marmotte";
5756 const char *value
= "milka";
5759 * Please note that I'm abusing the internal structures in libmemcached
5760 * in a non-portable way and you shouldn't be doing this. I'm only
5761 * doing this in order to verify that the library works the way it should
5763 uint32_t number_of_hosts
= memcached_server_count(memc
);
5764 memc
->number_of_hosts
= 1;
5766 /* Ensure that we are connected to the server by setting a value */
5767 rc
= memcached_set(memc
, key
, strlen(key
),
5768 value
, strlen(value
),
5769 (time_t)0, (uint32_t)0);
5770 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5773 instance
= memcached_server_instance_by_position(memc
, 0);
5774 /* The test is to see that the memcached_quit doesn't increase the
5775 * the server failure conter, so let's ensure that it is zero
5776 * before sending quit
5778 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5780 memcached_quit(memc
);
5782 /* Verify that it memcached_quit didn't increment the failure counter
5783 * Please note that this isn't bullet proof, because an error could
5786 test_true(instance
->server_failure_counter
== 0);
5788 /* restore the instance */
5789 memc
->number_of_hosts
= number_of_hosts
;
5791 return TEST_SUCCESS
;
5798 * Test that ensures mget_execute does not end into recursive calls that finally fails
5800 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5802 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5803 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5804 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5805 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5806 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5809 return TEST_SKIPPED
; // My MAC can't handle this test
5813 * I only want to hit _one_ server so I know the number of requests I'm
5814 * sending in the pipeline.
5816 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5817 memc
->number_of_hosts
= 1;
5818 size_t max_keys
= 20480;
5821 char **keys
= calloc(max_keys
, sizeof(char*));
5822 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5824 /* First add all of the items.. */
5826 char blob
[1024]= { 0 };
5827 memcached_return rc
;
5828 for (size_t x
= 0; x
< max_keys
; ++x
)
5831 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%zu", x
);
5833 assert(keys
[x
] != NULL
);
5834 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5836 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5838 break; // We are out of business
5841 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5843 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5846 sleep(1);// We will try to sleep
5849 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5851 // We failed to send everything.
5856 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5859 /* Try to get all of them with a large multiget */
5861 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5862 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5863 (size_t)max_keys
, callbacks
, &counter
, 1);
5865 assert(rc
== MEMCACHED_SUCCESS
);
5866 char* the_value
= NULL
;
5867 char the_key
[MEMCACHED_MAX_KEY
];
5868 size_t the_key_length
;
5869 size_t the_value_length
;
5873 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5875 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5881 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5884 assert(rc
== MEMCACHED_END
);
5886 /* Verify that we got all of the items */
5887 assert(counter
== max_keys
);
5890 /* Release all allocated resources */
5891 for (size_t x
= 0; x
< max_keys
; ++x
)
5898 memc
->number_of_hosts
= number_of_hosts
;
5900 return TEST_SUCCESS
;
5903 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5907 memcached_st
*memc
= memcached_create(NULL
);
5909 memcached_server_add(memc
, "10.2.3.4", 11211);
5911 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5912 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5913 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5914 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5915 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5916 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5918 memcached_return_t rc
;
5922 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5924 test_true(rc
== MEMCACHED_TIMEOUT
);
5926 memcached_free(memc
);
5928 return TEST_SUCCESS
;
5931 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5933 fprintf(stderr
, "Iteration #%u: ", it
);
5935 if(error
== MEMCACHED_ERRNO
)
5937 fprintf(stderr
, "system error %d from %s: %s\n",
5938 errno
, what
, strerror(errno
));
5942 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5943 memcached_strerror(mc
, error
));
5947 #define TEST_CONSTANT_CREATION 200
5949 static test_return_t
regression_bug_(memcached_st
*memc
)
5951 const char *remote_server
;
5954 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5956 return TEST_SKIPPED
;
5959 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5961 memcached_st
* mc
= memcached_create(NULL
);
5962 memcached_return rc
;
5964 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5965 if (rc
!= MEMCACHED_SUCCESS
)
5967 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5970 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5971 if (rc
!= MEMCACHED_SUCCESS
)
5973 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5976 rc
= memcached_server_add(mc
, remote_server
, 0);
5977 if (rc
!= MEMCACHED_SUCCESS
)
5979 memcached_die(mc
, rc
, "memcached_server_add", x
);
5982 const char *set_key
= "akey";
5983 const size_t set_key_len
= strlen(set_key
);
5984 const char *set_value
= "a value";
5985 const size_t set_value_len
= strlen(set_value
);
5987 if (rc
== MEMCACHED_SUCCESS
)
5991 size_t get_value_len
;
5993 uint32_t get_value_flags
;
5995 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5996 &get_value_flags
, &rc
);
5997 if (rc
!= MEMCACHED_SUCCESS
)
5999 memcached_die(mc
, rc
, "memcached_get", x
);
6005 (get_value_len
!= set_value_len
6006 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
6008 fprintf(stderr
, "Values don't match?\n");
6009 rc
= MEMCACHED_FAILURE
;
6015 rc
= memcached_set(mc
,
6016 set_key
, set_key_len
,
6017 set_value
, set_value_len
,
6021 if (rc
!= MEMCACHED_SUCCESS
)
6023 memcached_die(mc
, rc
, "memcached_set", x
);
6030 if (rc
!= MEMCACHED_SUCCESS
)
6036 return TEST_SUCCESS
;
6040 * Test that the sasl authentication works. We cannot use the default
6041 * pool of servers, because that would require that all servers we want
6042 * to test supports SASL authentication, and that they use the default
6045 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6047 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6048 memcached_return_t rc
;
6050 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6051 test_true(rc
== MEMCACHED_SUCCESS
);
6052 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6053 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6054 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6055 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6056 memcached_quit(memc
);
6058 rc
= memcached_set_sasl_auth_data(memc
,
6059 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6060 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6061 test_true(rc
== MEMCACHED_SUCCESS
);
6063 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6064 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6065 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6067 memcached_quit(memc
);
6068 return TEST_SUCCESS
;
6071 return TEST_FAILURE
;
6075 /* Clean the server before beginning testing */
6077 {"util_version", 1, (test_callback_fn
)util_version_test
},
6078 {"flush", 0, (test_callback_fn
)flush_test
},
6079 {"init", 0, (test_callback_fn
)init_test
},
6080 {"allocation", 0, (test_callback_fn
)allocation_test
},
6081 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6082 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6083 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6084 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6085 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6086 {"clone_test", 0, (test_callback_fn
)clone_test
},
6087 {"connection_test", 0, (test_callback_fn
)connection_test
},
6088 {"callback_test", 0, (test_callback_fn
)callback_test
},
6089 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6090 {"error", 0, (test_callback_fn
)error_test
},
6091 {"set", 0, (test_callback_fn
)set_test
},
6092 {"set2", 0, (test_callback_fn
)set_test2
},
6093 {"set3", 0, (test_callback_fn
)set_test3
},
6094 {"dump", 1, (test_callback_fn
)dump_test
},
6095 {"add", 1, (test_callback_fn
)add_test
},
6096 {"replace", 1, (test_callback_fn
)replace_test
},
6097 {"delete", 1, (test_callback_fn
)delete_test
},
6098 {"get", 1, (test_callback_fn
)get_test
},
6099 {"get2", 0, (test_callback_fn
)get_test2
},
6100 {"get3", 0, (test_callback_fn
)get_test3
},
6101 {"get4", 0, (test_callback_fn
)get_test4
},
6102 {"partial mget", 0, (test_callback_fn
)get_test5
},
6103 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6104 {"increment", 0, (test_callback_fn
)increment_test
},
6105 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6106 {"decrement", 0, (test_callback_fn
)decrement_test
},
6107 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6108 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6109 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6110 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6111 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6112 {"quit", 0, (test_callback_fn
)quit_test
},
6113 {"mget", 1, (test_callback_fn
)mget_test
},
6114 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6115 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6116 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6117 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6118 {"mget_end", 0, (test_callback_fn
)mget_end
},
6119 {"get_stats", 0, (test_callback_fn
)get_stats
},
6120 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6121 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6122 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6123 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6124 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6125 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6126 {"read_through", 1, (test_callback_fn
)read_through
},
6127 {"delete_through", 1, (test_callback_fn
)delete_through
},
6128 {"noreply", 1, (test_callback_fn
)noreply_test
},
6129 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6130 #ifdef HAVE_LIBMEMCACHEDUTIL
6131 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6132 {"ping", 1, (test_callback_fn
)ping_test
},
6134 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6135 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6136 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6137 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6141 test_st behavior_tests
[] ={
6142 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6143 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6144 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6145 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6149 test_st regression_binary_vs_block
[] ={
6150 {"block add", 1, (test_callback_fn
)block_add_regression
},
6151 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6155 test_st async_tests
[] ={
6156 {"add", 1, (test_callback_fn
)add_wrapper
},
6160 test_st string_tests
[] ={
6161 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6162 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6163 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6164 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6165 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6166 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6167 {0, 0, (test_callback_fn
)0}
6170 test_st result_tests
[] ={
6171 {"result static", 0, (test_callback_fn
)result_static
},
6172 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6173 {0, 0, (test_callback_fn
)0}
6176 test_st version_1_2_3
[] ={
6177 {"append", 0, (test_callback_fn
)append_test
},
6178 {"prepend", 0, (test_callback_fn
)prepend_test
},
6179 {"cas", 0, (test_callback_fn
)cas_test
},
6180 {"cas2", 0, (test_callback_fn
)cas2_test
},
6181 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6182 {0, 0, (test_callback_fn
)0}
6185 test_st user_tests
[] ={
6186 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6187 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6188 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6189 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6190 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6191 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6192 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6193 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6194 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6195 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6196 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6197 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6198 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6199 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6200 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6201 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6204 ** It seems to be something weird with the character sets..
6205 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6206 ** guess I need to find out how this is supposed to work.. Perhaps I need
6207 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6208 ** so just disable the code for now...).
6210 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6212 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6213 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6214 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6215 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6216 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6217 {0, 0, (test_callback_fn
)0}
6220 test_st replication_tests
[]= {
6221 {"set", 1, (test_callback_fn
)replication_set_test
},
6222 {"get", 0, (test_callback_fn
)replication_get_test
},
6223 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6224 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6225 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6226 {0, 0, (test_callback_fn
)0}
6230 * The following test suite is used to verify that we don't introduce
6231 * regression bugs. If you want more information about the bug / test,
6232 * you should look in the bug report at
6233 * http://bugs.launchpad.net/libmemcached
6235 test_st regression_tests
[]= {
6236 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6237 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6238 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6239 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6240 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6241 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6242 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6243 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6244 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6245 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6246 {0, 0, (test_callback_fn
)0}
6249 test_st sasl_auth_tests
[]= {
6250 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6251 {0, 0, (test_callback_fn
)0}
6254 test_st ketama_compatibility
[]= {
6255 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6256 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6257 {0, 0, (test_callback_fn
)0}
6260 test_st generate_tests
[] ={
6261 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6262 {"generate_data", 1, (test_callback_fn
)generate_data
},
6263 {"get_read", 0, (test_callback_fn
)get_read
},
6264 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6265 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6266 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6267 {"generate_data", 1, (test_callback_fn
)generate_data
},
6268 {"mget_read", 0, (test_callback_fn
)mget_read
},
6269 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6270 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6271 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6272 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6273 {"generate_data", 1, (test_callback_fn
)generate_data
},
6274 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6275 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6276 {0, 0, (test_callback_fn
)0}
6279 test_st consistent_tests
[] ={
6280 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6281 {"generate_data", 1, (test_callback_fn
)generate_data
},
6282 {"get_read", 0, (test_callback_fn
)get_read_count
},
6283 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6284 {0, 0, (test_callback_fn
)0}
6287 test_st consistent_weighted_tests
[] ={
6288 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6289 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6290 {"get_read", 0, (test_callback_fn
)get_read_count
},
6291 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6292 {0, 0, (test_callback_fn
)0}
6295 test_st hsieh_availability
[] ={
6296 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6297 {0, 0, (test_callback_fn
)0}
6301 test_st hash_sanity
[] ={
6302 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6303 {0, 0, (test_callback_fn
)0}
6307 test_st ketama_auto_eject_hosts
[] ={
6308 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6309 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6310 {0, 0, (test_callback_fn
)0}
6313 test_st hash_tests
[] ={
6314 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6315 {"md5", 0, (test_callback_fn
)md5_run
},
6316 {"crc", 0, (test_callback_fn
)crc_run
},
6317 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6318 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6319 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6320 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6321 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6322 {"murmur", 0, (test_callback_fn
)murmur_run
},
6323 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6324 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6325 {0, 0, (test_callback_fn
)0}
6328 test_st error_conditions
[] ={
6329 {"memcached_get_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6330 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6331 {"memcached_get_by_key_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6332 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6333 {0, 0, (test_callback_fn
)0}
6336 collection_st collection
[] ={
6338 {"hash_sanity", 0, 0, hash_sanity
},
6340 {"hsieh_availability", 0, 0, hsieh_availability
},
6341 {"block", 0, 0, tests
},
6342 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6343 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6344 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6345 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6346 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6347 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6348 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6349 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6350 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6351 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6352 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6353 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6354 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6355 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6356 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6357 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6358 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6359 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6360 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6361 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6362 #ifdef MEMCACHED_ENABLE_DEPRECATED
6363 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6365 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6366 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6367 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6368 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6369 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6370 {"string", 0, 0, string_tests
},
6371 {"result", 0, 0, result_tests
},
6372 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6373 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6374 {"user", 0, 0, user_tests
},
6375 {"generate", 0, 0, generate_tests
},
6376 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6377 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6378 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6379 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6380 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6381 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6382 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6384 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6385 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6386 {"consistent_not", 0, 0, consistent_tests
},
6387 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6388 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6389 {"ketama_compat", 0, 0, ketama_compatibility
},
6390 {"test_hashes", 0, 0, hash_tests
},
6391 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6392 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6393 {"regression", 0, 0, regression_tests
},
6394 {"behaviors", 0, 0, behavior_tests
},
6395 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6396 {"error_conditions", 0, 0, error_conditions
},
6400 #define SERVERS_TO_CREATE 5
6402 #include "libmemcached_world.h"
6404 void get_world(world_st
*world
)
6406 world
->collections
= collection
;
6408 world
->create
= (test_callback_create_fn
)world_create
;
6409 world
->destroy
= (test_callback_fn
)world_destroy
;
6411 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6412 world
->test
.flush
= (test_callback_fn
)world_flush
;
6413 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6414 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6415 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6417 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6418 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6420 world
->runner
= &defualt_libmemcached_runner
;