1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
21 #include <sys/types.h>
27 #include "libmemcached/common.h"
30 #include "clients/generator.h"
31 #include "clients/execute.h"
33 #define SMALL_STRING_LEN 1024
38 #ifdef HAVE_LIBMEMCACHEDUTIL
40 #include "libmemcached/memcached_util.h"
43 #include "hash_results.h"
45 #define GLOBAL_COUNT 10000
46 #define GLOBAL2_COUNT 100
47 #define SERVERS_TO_CREATE 5
48 static uint32_t global_count
;
50 static pairs_st
*global_pairs
;
51 static const char *global_keys
[GLOBAL_COUNT
];
52 static size_t global_keys_length
[GLOBAL_COUNT
];
55 static test_return_t
pre_binary(memcached_st
*memc
);
58 static test_return_t
init_test(memcached_st
*not_used
__attribute__((unused
)))
62 (void)memcached_create(&memc
);
63 memcached_free(&memc
);
68 static test_return_t
server_list_null_test(memcached_st
*ptr
__attribute__((unused
)))
70 memcached_server_st
*server_list
;
71 memcached_return_t rc
;
73 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, NULL
);
74 test_true(server_list
== NULL
);
76 server_list
= memcached_server_list_append_with_weight(NULL
, "localhost", 0, 0, NULL
);
77 test_true(server_list
== NULL
);
79 server_list
= memcached_server_list_append_with_weight(NULL
, NULL
, 0, 0, &rc
);
80 test_true(server_list
== NULL
);
85 #define TEST_PORT_COUNT 7
86 in_port_t test_ports
[TEST_PORT_COUNT
];
88 static memcached_return_t
server_display_function(const memcached_st
*ptr
__attribute__((unused
)),
89 const memcached_server_st
*server
,
93 size_t bigger
= *((size_t *)(context
));
94 assert(bigger
<= memcached_server_port(server
));
95 *((size_t *)(context
))= memcached_server_port(server
);
97 return MEMCACHED_SUCCESS
;
100 static memcached_return_t
dump_server_information(const memcached_st
*ptr
__attribute__((unused
)),
101 const memcached_server_st
*instance
,
105 FILE *stream
= (FILE *)context
;
107 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
108 memcached_server_name(instance
),
109 memcached_server_port(instance
),
110 instance
->major_version
,
111 instance
->minor_version
,
112 instance
->micro_version
);
114 return MEMCACHED_SUCCESS
;
117 static test_return_t
server_sort_test(memcached_st
*ptr
__attribute__((unused
)))
119 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
121 memcached_return_t rc
;
122 memcached_server_fn callbacks
[1];
123 memcached_st
*local_memc
;
125 local_memc
= memcached_create(NULL
);
126 test_true(local_memc
);
127 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
129 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
131 test_ports
[x
]= (in_port_t
)random() % 64000;
132 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
133 test_true(memcached_server_count(local_memc
) == x
+ 1);
135 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
137 test_true(rc
== MEMCACHED_SUCCESS
);
140 callbacks
[0]= server_display_function
;
141 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
144 memcached_free(local_memc
);
149 static test_return_t
server_sort2_test(memcached_st
*ptr
__attribute__((unused
)))
151 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
152 memcached_return_t rc
;
153 memcached_server_fn callbacks
[1];
154 memcached_st
*local_memc
;
155 memcached_server_instance_st instance
;
157 local_memc
= memcached_create(NULL
);
158 test_true(local_memc
);
159 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
160 test_true(rc
== MEMCACHED_SUCCESS
);
162 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
163 test_true(rc
== MEMCACHED_SUCCESS
);
164 instance
= memcached_server_instance_by_position(local_memc
, 0);
165 test_true(memcached_server_port(instance
) == 43043);
167 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
168 test_true(rc
== MEMCACHED_SUCCESS
);
170 instance
= memcached_server_instance_by_position(local_memc
, 0);
171 test_true(memcached_server_port(instance
) == 43042);
173 instance
= memcached_server_instance_by_position(local_memc
, 1);
174 test_true(memcached_server_port(instance
) == 43043);
176 callbacks
[0]= server_display_function
;
177 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
180 memcached_free(local_memc
);
185 static memcached_return_t
server_print_callback(const memcached_st
*ptr
__attribute__((unused
)),
186 const memcached_server_st
*server
,
187 void *context
__attribute__((unused
)))
189 (void)server
; // Just in case we aren't printing.
192 fprintf(stderr
, "%s(%d)", memcached_server_name(server
), memcached_server_port(server
));
195 return MEMCACHED_SUCCESS
;
198 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
__attribute__((unused
)))
200 memcached_return_t rc
;
201 memcached_st local_memc
;
203 memcached_server_st
*servers
;
204 memcached_server_fn callbacks
[1];
206 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";
208 memc
= memcached_create(&local_memc
);
210 servers
= memcached_servers_parse(server_string
);
212 rc
= memcached_server_push(memc
, servers
);
213 memcached_server_list_free(servers
);
215 callbacks
[0]= server_print_callback
;
216 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
218 memcached_free(memc
);
223 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
__attribute__((unused
)),
224 const memcached_server_st
*server
,
228 uint32_t x
= *((uint32_t *)(context
));
230 assert(test_ports
[x
] == server
->port
);
231 *((uint32_t *)(context
))= ++x
;
233 return MEMCACHED_SUCCESS
;
236 static test_return_t
server_unsort_test(memcached_st
*ptr
__attribute__((unused
)))
238 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
239 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
240 memcached_return_t rc
;
241 memcached_server_fn callbacks
[1];
242 memcached_st
*local_memc
;
244 local_memc
= memcached_create(NULL
);
245 test_true(local_memc
);
247 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
249 test_ports
[x
]= (in_port_t
)(random() % 64000);
250 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
251 test_true(memcached_server_count(local_memc
) == x
+1);
253 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
255 test_true(rc
== MEMCACHED_SUCCESS
);
258 callbacks
[0]= server_display_unsort_function
;
259 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
261 /* Now we sort old data! */
262 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
263 callbacks
[0]= server_display_function
;
264 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
267 memcached_free(local_memc
);
272 static test_return_t
allocation_test(memcached_st
*not_used
__attribute__((unused
)))
275 memc
= memcached_create(NULL
);
277 memcached_free(memc
);
282 static test_return_t
clone_test(memcached_st
*memc
)
286 memcached_st
*memc_clone
;
287 memc_clone
= memcached_clone(NULL
, NULL
);
288 test_true(memc_clone
);
289 memcached_free(memc_clone
);
292 /* Can we init from null? */
294 memcached_st
*memc_clone
;
295 memc_clone
= memcached_clone(NULL
, memc
);
296 test_true(memc_clone
);
299 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
300 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
301 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
302 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
305 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
306 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
307 test_true(memc_clone
->distribution
== memc
->distribution
);
308 { // Test all of the flags
309 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
310 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
311 test_true(memc_clone
->flags
.reuse_memory
== memc
->flags
.reuse_memory
);
312 test_true(memc_clone
->flags
.use_cache_lookups
== memc
->flags
.use_cache_lookups
);
313 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
314 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
315 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
316 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
317 test_true(memc_clone
->flags
.ketama_weighted
== memc
->flags
.ketama_weighted
);
318 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
319 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
320 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
321 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
322 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
323 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
325 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
326 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
327 test_true(hashkit_compare(&memc_clone
->distribution_hashkit
, &memc
->distribution_hashkit
));
328 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
329 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
330 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
331 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
332 test_true(memc_clone
->on_clone
== memc
->on_clone
);
333 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
334 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
335 test_true(memc_clone
->recv_size
== memc
->recv_size
);
336 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
337 test_true(memc_clone
->send_size
== memc
->send_size
);
338 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
339 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
340 test_true(memc_clone
->user_data
== memc
->user_data
);
342 memcached_free(memc_clone
);
345 /* Can we init from struct? */
347 memcached_st declared_clone
;
348 memcached_st
*memc_clone
;
349 memset(&declared_clone
, 0 , sizeof(memcached_st
));
350 memc_clone
= memcached_clone(&declared_clone
, NULL
);
351 test_true(memc_clone
);
352 memcached_free(memc_clone
);
355 /* Can we init from struct? */
357 memcached_st declared_clone
;
358 memcached_st
*memc_clone
;
359 memset(&declared_clone
, 0 , sizeof(memcached_st
));
360 memc_clone
= memcached_clone(&declared_clone
, memc
);
361 test_true(memc_clone
);
362 memcached_free(memc_clone
);
368 static test_return_t
userdata_test(memcached_st
*memc
)
371 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
372 test_true(memcached_get_user_data(memc
) == foo
);
373 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
378 static test_return_t
connection_test(memcached_st
*memc
)
380 memcached_return_t rc
;
382 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
383 test_true(rc
== MEMCACHED_SUCCESS
);
388 static test_return_t
error_test(memcached_st
*memc
)
390 memcached_return_t rc
;
391 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
392 982370485U, 1263635348U, 4242906218U, 3829656100U,
393 1891735253U, 334139633U, 2257084983U, 3088286104U,
394 13199785U, 2542027183U, 1097051614U, 199566778U,
395 2748246961U, 2465192557U, 1664094137U, 2405439045U,
396 1842224848U, 692413798U, 3479807801U, 919913813U,
397 4269430871U, 610793021U, 527273862U, 1437122909U,
398 2300930706U, 2943759320U, 674306647U, 2400528935U,
399 54481931U, 4186304426U, 1741088401U, 2979625118U,
400 4159057246U, 3425930182U, 2593724503U, 1868899624U,
401 1769812374U, 2302537950U, 1110330676U };
403 // You have updated the memcache_error messages but not updated docs/tests.
404 test_true(MEMCACHED_MAXIMUM_RETURN
== 43);
405 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
408 const char *msg
= memcached_strerror(memc
, rc
);
409 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
410 MEMCACHED_HASH_JENKINS
);
411 if (values
[rc
] != hash_val
)
413 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
414 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
416 test_true(values
[rc
] == hash_val
);
422 static test_return_t
set_test(memcached_st
*memc
)
424 memcached_return_t rc
;
425 const char *key
= "foo";
426 const char *value
= "when we sanitize";
428 rc
= memcached_set(memc
, key
, strlen(key
),
429 value
, strlen(value
),
430 (time_t)0, (uint32_t)0);
431 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
436 static test_return_t
append_test(memcached_st
*memc
)
438 memcached_return_t rc
;
439 const char *key
= "fig";
440 const char *in_value
= "we";
441 char *out_value
= NULL
;
445 rc
= memcached_flush(memc
, 0);
446 test_true(rc
== MEMCACHED_SUCCESS
);
448 rc
= memcached_set(memc
, key
, strlen(key
),
449 in_value
, strlen(in_value
),
450 (time_t)0, (uint32_t)0);
451 test_true(rc
== MEMCACHED_SUCCESS
);
453 rc
= memcached_append(memc
, key
, strlen(key
),
454 " the", strlen(" the"),
455 (time_t)0, (uint32_t)0);
456 test_true(rc
== MEMCACHED_SUCCESS
);
458 rc
= memcached_append(memc
, key
, strlen(key
),
459 " people", strlen(" people"),
460 (time_t)0, (uint32_t)0);
461 test_true(rc
== MEMCACHED_SUCCESS
);
463 out_value
= memcached_get(memc
, key
, strlen(key
),
464 &value_length
, &flags
, &rc
);
465 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
466 test_true(strlen("we the people") == value_length
);
467 test_true(rc
== MEMCACHED_SUCCESS
);
473 static test_return_t
append_binary_test(memcached_st
*memc
)
475 memcached_return_t rc
;
476 const char *key
= "numbers";
477 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
483 rc
= memcached_flush(memc
, 0);
484 test_true(rc
== MEMCACHED_SUCCESS
);
486 rc
= memcached_set(memc
,
489 (time_t)0, (uint32_t)0);
490 test_true(rc
== MEMCACHED_SUCCESS
);
492 for (x
= 0; store_list
[x
] ; x
++)
494 rc
= memcached_append(memc
,
496 (char *)&store_list
[x
], sizeof(uint32_t),
497 (time_t)0, (uint32_t)0);
498 test_true(rc
== MEMCACHED_SUCCESS
);
501 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
502 &value_length
, &flags
, &rc
);
503 test_true((value_length
== (sizeof(uint32_t) * x
)));
504 test_true(rc
== MEMCACHED_SUCCESS
);
506 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
508 test_true(*ptr
== store_list
[x
- counter
]);
516 static test_return_t
cas2_test(memcached_st
*memc
)
518 memcached_return_t rc
;
519 const char *keys
[]= {"fudge", "son", "food"};
520 size_t key_length
[]= {5, 3, 4};
521 const char *value
= "we the people";
522 size_t value_length
= strlen("we the people");
524 memcached_result_st results_obj
;
525 memcached_result_st
*results
;
528 rc
= memcached_flush(memc
, 0);
529 test_true(rc
== MEMCACHED_SUCCESS
);
531 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
533 for (x
= 0; x
< 3; x
++)
535 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
536 keys
[x
], key_length
[x
],
537 (time_t)50, (uint32_t)9);
538 test_true(rc
== MEMCACHED_SUCCESS
);
541 rc
= memcached_mget(memc
, keys
, key_length
, 3);
543 results
= memcached_result_create(memc
, &results_obj
);
545 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
547 test_true(results
->item_cas
);
548 test_true(rc
== MEMCACHED_SUCCESS
);
549 test_true(memcached_result_cas(results
));
551 test_true(!memcmp(value
, "we the people", strlen("we the people")));
552 test_true(strlen("we the people") == value_length
);
553 test_true(rc
== MEMCACHED_SUCCESS
);
555 memcached_result_free(&results_obj
);
560 static test_return_t
cas_test(memcached_st
*memc
)
562 memcached_return_t rc
;
563 const char *key
= "fun";
564 size_t key_length
= strlen(key
);
565 const char *value
= "we the people";
566 const char* keys
[2] = { key
, NULL
};
567 size_t keylengths
[2] = { strlen(key
), 0 };
568 size_t value_length
= strlen(value
);
569 const char *value2
= "change the value";
570 size_t value2_length
= strlen(value2
);
572 memcached_result_st results_obj
;
573 memcached_result_st
*results
;
576 rc
= memcached_flush(memc
, 0);
577 test_true(rc
== MEMCACHED_SUCCESS
);
579 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
581 rc
= memcached_set(memc
, key
, strlen(key
),
582 value
, strlen(value
),
583 (time_t)0, (uint32_t)0);
584 test_true(rc
== MEMCACHED_SUCCESS
);
586 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
588 results
= memcached_result_create(memc
, &results_obj
);
590 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
592 test_true(rc
== MEMCACHED_SUCCESS
);
593 test_true(memcached_result_cas(results
));
594 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
595 test_true(strlen(memcached_result_value(results
)) == value_length
);
596 test_true(rc
== MEMCACHED_SUCCESS
);
597 uint64_t cas
= memcached_result_cas(results
);
600 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
601 test_true(rc
== MEMCACHED_END
);
602 test_true(results
== NULL
);
605 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
606 test_true(rc
== MEMCACHED_SUCCESS
);
609 * The item will have a new cas value, so try to set it again with the old
610 * value. This should fail!
612 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
613 test_true(rc
== MEMCACHED_DATA_EXISTS
);
615 memcached_result_free(&results_obj
);
620 static test_return_t
prepend_test(memcached_st
*memc
)
622 memcached_return_t rc
;
623 const char *key
= "fig";
624 const char *value
= "people";
625 char *out_value
= NULL
;
629 rc
= memcached_flush(memc
, 0);
630 test_true(rc
== MEMCACHED_SUCCESS
);
632 rc
= memcached_set(memc
, key
, strlen(key
),
633 value
, strlen(value
),
634 (time_t)0, (uint32_t)0);
635 test_true(rc
== MEMCACHED_SUCCESS
);
637 rc
= memcached_prepend(memc
, key
, strlen(key
),
638 "the ", strlen("the "),
639 (time_t)0, (uint32_t)0);
640 test_true(rc
== MEMCACHED_SUCCESS
);
642 rc
= memcached_prepend(memc
, key
, strlen(key
),
643 "we ", strlen("we "),
644 (time_t)0, (uint32_t)0);
645 test_true(rc
== MEMCACHED_SUCCESS
);
647 out_value
= memcached_get(memc
, key
, strlen(key
),
648 &value_length
, &flags
, &rc
);
649 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
650 test_true(strlen("we the people") == value_length
);
651 test_true(rc
== MEMCACHED_SUCCESS
);
658 Set the value, then quit to make sure it is flushed.
659 Come back in and test that add fails.
661 static test_return_t
add_test(memcached_st
*memc
)
663 memcached_return_t rc
;
664 const char *key
= "foo";
665 const char *value
= "when we sanitize";
666 unsigned long long setting_value
;
668 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
670 rc
= memcached_set(memc
, key
, strlen(key
),
671 value
, strlen(value
),
672 (time_t)0, (uint32_t)0);
673 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
674 memcached_quit(memc
);
675 rc
= memcached_add(memc
, key
, strlen(key
),
676 value
, strlen(value
),
677 (time_t)0, (uint32_t)0);
679 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
682 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
686 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
693 ** There was a problem of leaking filedescriptors in the initial release
694 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
695 ** systems it seems that the kernel is slow on reclaiming the resources
696 ** because the connects starts to time out (the test doesn't do much
697 ** anyway, so just loop 10 iterations)
699 static test_return_t
add_wrapper(memcached_st
*memc
)
701 unsigned int max
= 10000;
709 for (uint32_t x
= 0; x
< max
; x
++)
715 static test_return_t
replace_test(memcached_st
*memc
)
717 memcached_return_t rc
;
718 const char *key
= "foo";
719 const char *value
= "when we sanitize";
720 const char *original
= "first we insert some data";
722 rc
= memcached_set(memc
, key
, strlen(key
),
723 original
, strlen(original
),
724 (time_t)0, (uint32_t)0);
725 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
727 rc
= memcached_replace(memc
, key
, strlen(key
),
728 value
, strlen(value
),
729 (time_t)0, (uint32_t)0);
730 test_true(rc
== MEMCACHED_SUCCESS
);
735 static test_return_t
delete_test(memcached_st
*memc
)
737 memcached_return_t rc
;
738 const char *key
= "foo";
739 const char *value
= "when we sanitize";
741 rc
= memcached_set(memc
, key
, strlen(key
),
742 value
, strlen(value
),
743 (time_t)0, (uint32_t)0);
744 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
746 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
747 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
752 static test_return_t
flush_test(memcached_st
*memc
)
754 memcached_return_t rc
;
756 rc
= memcached_flush(memc
, 0);
757 test_true(rc
== MEMCACHED_SUCCESS
);
762 static memcached_return_t
server_function(const memcached_st
*ptr
__attribute__((unused
)),
763 const memcached_server_st
*server
__attribute__((unused
)),
764 void *context
__attribute__((unused
)))
768 return MEMCACHED_SUCCESS
;
771 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
774 strcpy(context
, "foo bad");
775 memcached_server_fn callbacks
[1];
777 callbacks
[0]= server_function
;
778 memcached_server_cursor(memc
, callbacks
, context
, 1);
782 static test_return_t
bad_key_test(memcached_st
*memc
)
784 memcached_return_t rc
;
785 const char *key
= "foo bad";
787 size_t string_length
;
789 memcached_st
*memc_clone
;
791 size_t max_keylen
= 0xffff;
793 // Just skip if we are in binary mode.
794 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
797 memc_clone
= memcached_clone(NULL
, memc
);
798 test_true(memc_clone
);
800 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
801 test_true(rc
== MEMCACHED_SUCCESS
);
803 /* All keys are valid in the binary protocol (except for length) */
804 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
806 string
= memcached_get(memc_clone
, key
, strlen(key
),
807 &string_length
, &flags
, &rc
);
808 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
809 test_true(string_length
== 0);
813 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
814 test_true(rc
== MEMCACHED_SUCCESS
);
815 string
= memcached_get(memc_clone
, key
, strlen(key
),
816 &string_length
, &flags
, &rc
);
817 test_true(rc
== MEMCACHED_NOTFOUND
);
818 test_true(string_length
== 0);
821 /* Test multi key for bad keys */
822 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
823 size_t key_lengths
[] = { 7, 7, 7 };
825 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
826 test_true(rc
== MEMCACHED_SUCCESS
);
828 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
829 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
831 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
832 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
836 /* The following test should be moved to the end of this function when the
837 memcached server is updated to allow max size length of the keys in the
840 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
841 test_true(rc
== MEMCACHED_SUCCESS
);
843 char *longkey
= malloc(max_keylen
+ 1);
846 memset(longkey
, 'a', max_keylen
+ 1);
847 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
848 &string_length
, &flags
, &rc
);
849 test_true(rc
== MEMCACHED_NOTFOUND
);
850 test_true(string_length
== 0);
853 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
854 &string_length
, &flags
, &rc
);
855 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
856 test_true(string_length
== 0);
863 /* Make sure zero length keys are marked as bad */
865 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
866 test_true(rc
== MEMCACHED_SUCCESS
);
867 string
= memcached_get(memc_clone
, key
, 0,
868 &string_length
, &flags
, &rc
);
869 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
870 test_true(string_length
== 0);
873 memcached_free(memc_clone
);
878 #define READ_THROUGH_VALUE "set for me"
879 static memcached_return_t
read_through_trigger(memcached_st
*memc
__attribute__((unused
)),
880 char *key
__attribute__((unused
)),
881 size_t key_length
__attribute__((unused
)),
882 memcached_result_st
*result
)
885 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
888 static test_return_t
read_through(memcached_st
*memc
)
890 memcached_return_t rc
;
891 const char *key
= "foo";
893 size_t string_length
;
895 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
897 string
= memcached_get(memc
, key
, strlen(key
),
898 &string_length
, &flags
, &rc
);
900 test_true(rc
== MEMCACHED_NOTFOUND
);
901 test_false(string_length
);
904 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
906 test_true(rc
== MEMCACHED_SUCCESS
);
908 string
= memcached_get(memc
, key
, strlen(key
),
909 &string_length
, &flags
, &rc
);
911 test_true(rc
== MEMCACHED_SUCCESS
);
912 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
913 test_strcmp(READ_THROUGH_VALUE
, string
);
916 string
= memcached_get(memc
, key
, strlen(key
),
917 &string_length
, &flags
, &rc
);
919 test_true(rc
== MEMCACHED_SUCCESS
);
920 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
921 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
927 static memcached_return_t
delete_trigger(memcached_st
*ptr
__attribute__((unused
)),
929 size_t key_length
__attribute__((unused
)))
933 return MEMCACHED_SUCCESS
;
936 static test_return_t
delete_through(memcached_st
*memc
)
938 memcached_trigger_delete_key_fn callback
;
939 memcached_return_t rc
;
941 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
943 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
944 test_true(rc
== MEMCACHED_SUCCESS
);
949 static test_return_t
get_test(memcached_st
*memc
)
951 memcached_return_t rc
;
952 const char *key
= "foo";
954 size_t string_length
;
957 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
958 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
960 string
= memcached_get(memc
, key
, strlen(key
),
961 &string_length
, &flags
, &rc
);
963 test_true(rc
== MEMCACHED_NOTFOUND
);
964 test_false(string_length
);
970 static test_return_t
get_test2(memcached_st
*memc
)
972 memcached_return_t rc
;
973 const char *key
= "foo";
974 const char *value
= "when we sanitize";
976 size_t string_length
;
979 rc
= memcached_set(memc
, key
, strlen(key
),
980 value
, strlen(value
),
981 (time_t)0, (uint32_t)0);
982 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
984 string
= memcached_get(memc
, key
, strlen(key
),
985 &string_length
, &flags
, &rc
);
988 test_true(rc
== MEMCACHED_SUCCESS
);
989 test_true(string_length
== strlen(value
));
990 test_true(!memcmp(string
, value
, string_length
));
997 static test_return_t
set_test2(memcached_st
*memc
)
999 memcached_return_t rc
;
1000 const char *key
= "foo";
1001 const char *value
= "train in the brain";
1002 size_t value_length
= strlen(value
);
1005 for (x
= 0; x
< 10; x
++)
1007 rc
= memcached_set(memc
, key
, strlen(key
),
1008 value
, value_length
,
1009 (time_t)0, (uint32_t)0);
1010 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1013 return TEST_SUCCESS
;
1016 static test_return_t
set_test3(memcached_st
*memc
)
1018 memcached_return_t rc
;
1020 size_t value_length
= 8191;
1023 value
= (char*)malloc(value_length
);
1026 for (x
= 0; x
< value_length
; x
++)
1027 value
[x
] = (char) (x
% 127);
1029 /* The dump test relies on there being at least 32 items in memcached */
1030 for (x
= 0; x
< 32; x
++)
1034 snprintf(key
, sizeof(key
), "foo%u", x
);
1036 rc
= memcached_set(memc
, key
, strlen(key
),
1037 value
, value_length
,
1038 (time_t)0, (uint32_t)0);
1039 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1044 return TEST_SUCCESS
;
1047 static test_return_t
get_test3(memcached_st
*memc
)
1049 memcached_return_t rc
;
1050 const char *key
= "foo";
1052 size_t value_length
= 8191;
1054 size_t string_length
;
1058 value
= (char*)malloc(value_length
);
1061 for (x
= 0; x
< value_length
; x
++)
1062 value
[x
] = (char) (x
% 127);
1064 rc
= memcached_set(memc
, key
, strlen(key
),
1065 value
, value_length
,
1066 (time_t)0, (uint32_t)0);
1067 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1069 string
= memcached_get(memc
, key
, strlen(key
),
1070 &string_length
, &flags
, &rc
);
1072 test_true(rc
== MEMCACHED_SUCCESS
);
1074 test_true(string_length
== value_length
);
1075 test_true(!memcmp(string
, value
, string_length
));
1080 return TEST_SUCCESS
;
1083 static test_return_t
get_test4(memcached_st
*memc
)
1085 memcached_return_t rc
;
1086 const char *key
= "foo";
1088 size_t value_length
= 8191;
1090 size_t string_length
;
1094 value
= (char*)malloc(value_length
);
1097 for (x
= 0; x
< value_length
; x
++)
1098 value
[x
] = (char) (x
% 127);
1100 rc
= memcached_set(memc
, key
, strlen(key
),
1101 value
, value_length
,
1102 (time_t)0, (uint32_t)0);
1103 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1105 for (x
= 0; x
< 10; x
++)
1107 string
= memcached_get(memc
, key
, strlen(key
),
1108 &string_length
, &flags
, &rc
);
1110 test_true(rc
== MEMCACHED_SUCCESS
);
1112 test_true(string_length
== value_length
);
1113 test_true(!memcmp(string
, value
, string_length
));
1119 return TEST_SUCCESS
;
1123 * This test verifies that memcached_read_one_response doesn't try to
1124 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1125 * responses before you execute a storage command.
1127 static test_return_t
get_test5(memcached_st
*memc
)
1130 ** Request the same key twice, to ensure that we hash to the same server
1131 ** (so that we have multiple response values queued up) ;-)
1133 const char *keys
[]= { "key", "key" };
1134 size_t lengths
[]= { 3, 3 };
1138 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1139 keys
[0], lengths
[0], 0, 0);
1140 test_true(rc
== MEMCACHED_SUCCESS
);
1141 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1143 memcached_result_st results_obj
;
1144 memcached_result_st
*results
;
1145 results
=memcached_result_create(memc
, &results_obj
);
1147 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1149 memcached_result_free(&results_obj
);
1151 /* Don't read out the second result, but issue a set instead.. */
1152 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1153 test_true(rc
== MEMCACHED_SUCCESS
);
1155 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1156 &rlen
, &flags
, &rc
);
1157 test_true(val
== NULL
);
1158 test_true(rc
== MEMCACHED_NOTFOUND
);
1159 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1160 test_true(val
!= NULL
);
1161 test_true(rc
== MEMCACHED_SUCCESS
);
1164 return TEST_SUCCESS
;
1167 static test_return_t
mget_end(memcached_st
*memc
)
1169 const char *keys
[]= { "foo", "foo2" };
1170 size_t lengths
[]= { 3, 4 };
1171 const char *values
[]= { "fjord", "41" };
1173 memcached_return_t rc
;
1176 for (int i
= 0; i
< 2; i
++)
1178 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1179 (time_t)0, (uint32_t)0);
1180 test_true(rc
== MEMCACHED_SUCCESS
);
1184 size_t string_length
;
1187 // retrieve both via mget
1188 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1189 test_true(rc
== MEMCACHED_SUCCESS
);
1191 char key
[MEMCACHED_MAX_KEY
];
1194 // this should get both
1195 for (int i
= 0; i
< 2; i
++)
1197 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1199 test_true(rc
== MEMCACHED_SUCCESS
);
1201 if (key_length
== 4)
1203 test_true(string_length
== strlen(values
[val
]));
1204 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1208 // this should indicate end
1209 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1210 test_true(rc
== MEMCACHED_END
);
1213 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1214 test_true(rc
== MEMCACHED_SUCCESS
);
1216 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1217 test_true(key_length
== lengths
[0]);
1218 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1219 test_true(string_length
== strlen(values
[0]));
1220 test_true(strncmp(values
[0], string
, string_length
) == 0);
1221 test_true(rc
== MEMCACHED_SUCCESS
);
1224 // this should indicate end
1225 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1226 test_true(rc
== MEMCACHED_END
);
1228 return TEST_SUCCESS
;
1231 /* Do not copy the style of this code, I just access hosts to testthis function */
1232 static test_return_t
stats_servername_test(memcached_st
*memc
)
1234 memcached_return_t rc
;
1235 memcached_stat_st memc_stat
;
1236 memcached_server_instance_st instance
=
1237 memcached_server_instance_by_position(memc
, 0);
1239 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1240 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1241 return TEST_SKIPPED
;
1243 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1244 memcached_server_name(instance
),
1245 memcached_server_port(instance
));
1247 return TEST_SUCCESS
;
1250 static test_return_t
increment_test(memcached_st
*memc
)
1252 uint64_t new_number
;
1253 memcached_return_t rc
;
1254 const char *key
= "number";
1255 const char *value
= "0";
1257 rc
= memcached_set(memc
, key
, strlen(key
),
1258 value
, strlen(value
),
1259 (time_t)0, (uint32_t)0);
1260 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1262 rc
= memcached_increment(memc
, key
, strlen(key
),
1264 test_true(rc
== MEMCACHED_SUCCESS
);
1265 test_true(new_number
== 1);
1267 rc
= memcached_increment(memc
, key
, strlen(key
),
1269 test_true(rc
== MEMCACHED_SUCCESS
);
1270 test_true(new_number
== 2);
1272 return TEST_SUCCESS
;
1275 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1277 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1279 uint64_t new_number
;
1280 memcached_return_t rc
;
1281 const char *key
= "number";
1282 uint64_t initial
= 0;
1284 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1285 1, initial
, 0, &new_number
);
1286 test_true(rc
== MEMCACHED_SUCCESS
);
1287 test_true(new_number
== initial
);
1289 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1290 1, initial
, 0, &new_number
);
1291 test_true(rc
== MEMCACHED_SUCCESS
);
1292 test_true(new_number
== (initial
+ 1));
1294 return TEST_SUCCESS
;
1297 static test_return_t
decrement_test(memcached_st
*memc
)
1299 uint64_t new_number
;
1300 memcached_return_t rc
;
1301 const char *key
= "number";
1302 const char *value
= "3";
1304 rc
= memcached_set(memc
, key
, strlen(key
),
1305 value
, strlen(value
),
1306 (time_t)0, (uint32_t)0);
1307 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1309 rc
= memcached_decrement(memc
, key
, strlen(key
),
1311 test_true(rc
== MEMCACHED_SUCCESS
);
1312 test_true(new_number
== 2);
1314 rc
= memcached_decrement(memc
, key
, strlen(key
),
1316 test_true(rc
== MEMCACHED_SUCCESS
);
1317 test_true(new_number
== 1);
1319 return TEST_SUCCESS
;
1322 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1324 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1326 uint64_t new_number
;
1327 memcached_return_t rc
;
1328 const char *key
= "number";
1329 uint64_t initial
= 3;
1331 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1332 1, initial
, 0, &new_number
);
1333 test_true(rc
== MEMCACHED_SUCCESS
);
1334 test_true(new_number
== initial
);
1336 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1337 1, initial
, 0, &new_number
);
1338 test_true(rc
== MEMCACHED_SUCCESS
);
1339 test_true(new_number
== (initial
- 1));
1341 return TEST_SUCCESS
;
1344 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1346 uint64_t new_number
;
1347 memcached_return_t rc
;
1348 const char *master_key
= "foo";
1349 const char *key
= "number";
1350 const char *value
= "0";
1352 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1354 value
, strlen(value
),
1355 (time_t)0, (uint32_t)0);
1356 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1358 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1360 test_true(rc
== MEMCACHED_SUCCESS
);
1361 test_true(new_number
== 1);
1363 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1365 test_true(rc
== MEMCACHED_SUCCESS
);
1366 test_true(new_number
== 2);
1368 return TEST_SUCCESS
;
1371 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1373 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1375 uint64_t new_number
;
1376 memcached_return_t rc
;
1377 const char *master_key
= "foo";
1378 const char *key
= "number";
1379 uint64_t initial
= 0;
1381 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1383 1, initial
, 0, &new_number
);
1384 test_true(rc
== MEMCACHED_SUCCESS
);
1385 test_true(new_number
== initial
);
1387 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1389 1, initial
, 0, &new_number
);
1390 test_true(rc
== MEMCACHED_SUCCESS
);
1391 test_true(new_number
== (initial
+ 1));
1393 return TEST_SUCCESS
;
1396 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1398 uint64_t new_number
;
1399 memcached_return_t rc
;
1400 const char *master_key
= "foo";
1401 const char *key
= "number";
1402 const char *value
= "3";
1404 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1406 value
, strlen(value
),
1407 (time_t)0, (uint32_t)0);
1408 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1410 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1413 test_true(rc
== MEMCACHED_SUCCESS
);
1414 test_true(new_number
== 2);
1416 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1419 test_true(rc
== MEMCACHED_SUCCESS
);
1420 test_true(new_number
== 1);
1422 return TEST_SUCCESS
;
1425 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1427 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1429 uint64_t new_number
;
1430 memcached_return_t rc
;
1431 const char *master_key
= "foo";
1432 const char *key
= "number";
1433 uint64_t initial
= 3;
1435 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1437 1, initial
, 0, &new_number
);
1438 test_true(rc
== MEMCACHED_SUCCESS
);
1439 test_true(new_number
== initial
);
1441 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1443 1, initial
, 0, &new_number
);
1444 test_true(rc
== MEMCACHED_SUCCESS
);
1445 test_true(new_number
== (initial
- 1));
1447 return TEST_SUCCESS
;
1450 static test_return_t
quit_test(memcached_st
*memc
)
1452 memcached_return_t rc
;
1453 const char *key
= "fudge";
1454 const char *value
= "sanford and sun";
1456 rc
= memcached_set(memc
, key
, strlen(key
),
1457 value
, strlen(value
),
1458 (time_t)10, (uint32_t)3);
1459 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1460 memcached_quit(memc
);
1462 rc
= memcached_set(memc
, key
, strlen(key
),
1463 value
, strlen(value
),
1464 (time_t)50, (uint32_t)9);
1465 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1467 return TEST_SUCCESS
;
1470 static test_return_t
mget_result_test(memcached_st
*memc
)
1472 memcached_return_t rc
;
1473 const char *keys
[]= {"fudge", "son", "food"};
1474 size_t key_length
[]= {5, 3, 4};
1477 memcached_result_st results_obj
;
1478 memcached_result_st
*results
;
1480 results
= memcached_result_create(memc
, &results_obj
);
1482 test_true(&results_obj
== results
);
1484 /* We need to empty the server before continueing test */
1485 rc
= memcached_flush(memc
, 0);
1486 test_true(rc
== MEMCACHED_SUCCESS
);
1488 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1489 test_true(rc
== MEMCACHED_SUCCESS
);
1491 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1496 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1497 test_true(!results
);
1498 test_true(rc
== MEMCACHED_END
);
1500 for (x
= 0; x
< 3; x
++)
1502 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1503 keys
[x
], key_length
[x
],
1504 (time_t)50, (uint32_t)9);
1505 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1508 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1509 test_true(rc
== MEMCACHED_SUCCESS
);
1511 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1514 test_true(&results_obj
== results
);
1515 test_true(rc
== MEMCACHED_SUCCESS
);
1516 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1517 test_true(!memcmp(memcached_result_key_value(results
),
1518 memcached_result_value(results
),
1519 memcached_result_length(results
)));
1522 memcached_result_free(&results_obj
);
1524 return TEST_SUCCESS
;
1527 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1529 memcached_return_t rc
;
1530 const char *keys
[]= {"fudge", "son", "food"};
1531 size_t key_length
[]= {5, 3, 4};
1534 memcached_result_st
*results
;
1536 /* We need to empty the server before continueing test */
1537 rc
= memcached_flush(memc
, 0);
1538 test_true(rc
== MEMCACHED_SUCCESS
);
1540 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1541 test_true(rc
== MEMCACHED_SUCCESS
);
1543 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1547 test_true(!results
);
1548 test_true(rc
== MEMCACHED_END
);
1550 for (x
= 0; x
< 3; x
++)
1552 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1553 keys
[x
], key_length
[x
],
1554 (time_t)50, (uint32_t)9);
1555 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1558 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1559 test_true(rc
== MEMCACHED_SUCCESS
);
1562 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1565 test_true(rc
== MEMCACHED_SUCCESS
);
1566 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1567 test_true(!memcmp(memcached_result_key_value(results
),
1568 memcached_result_value(results
),
1569 memcached_result_length(results
)));
1570 memcached_result_free(results
);
1574 return TEST_SUCCESS
;
1577 /* Count the results */
1578 static memcached_return_t
callback_counter(const memcached_st
*ptr
__attribute__((unused
)),
1579 memcached_result_st
*result
__attribute__((unused
)),
1582 size_t *counter
= (size_t *)context
;
1584 *counter
= *counter
+ 1;
1586 return MEMCACHED_SUCCESS
;
1589 static test_return_t
mget_result_function(memcached_st
*memc
)
1591 memcached_return_t rc
;
1592 const char *keys
[]= {"fudge", "son", "food"};
1593 size_t key_length
[]= {5, 3, 4};
1596 memcached_execute_fn callbacks
[1];
1598 /* We need to empty the server before continueing test */
1599 rc
= memcached_flush(memc
, 0);
1600 for (x
= 0; x
< 3; x
++)
1602 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1603 keys
[x
], key_length
[x
],
1604 (time_t)50, (uint32_t)9);
1605 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1608 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1609 test_true(rc
== MEMCACHED_SUCCESS
);
1611 callbacks
[0]= &callback_counter
;
1613 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1615 test_true(counter
== 3);
1617 return TEST_SUCCESS
;
1620 static test_return_t
mget_test(memcached_st
*memc
)
1622 memcached_return_t rc
;
1623 const char *keys
[]= {"fudge", "son", "food"};
1624 size_t key_length
[]= {5, 3, 4};
1628 char return_key
[MEMCACHED_MAX_KEY
];
1629 size_t return_key_length
;
1631 size_t return_value_length
;
1633 /* We need to empty the server before continueing test */
1634 rc
= memcached_flush(memc
, 0);
1635 test_true(rc
== MEMCACHED_SUCCESS
);
1637 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1638 test_true(rc
== MEMCACHED_SUCCESS
);
1640 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1641 &return_value_length
, &flags
, &rc
)) != NULL
)
1643 test_true(return_value
);
1645 test_true(!return_value
);
1646 test_true(return_value_length
== 0);
1647 test_true(rc
== MEMCACHED_END
);
1649 for (x
= 0; x
< 3; x
++)
1651 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1652 keys
[x
], key_length
[x
],
1653 (time_t)50, (uint32_t)9);
1654 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1657 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1658 test_true(rc
== MEMCACHED_SUCCESS
);
1661 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1662 &return_value_length
, &flags
, &rc
)))
1664 test_true(return_value
);
1665 test_true(rc
== MEMCACHED_SUCCESS
);
1666 test_true(return_key_length
== return_value_length
);
1667 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1672 return TEST_SUCCESS
;
1675 static test_return_t
mget_execute(memcached_st
*memc
)
1679 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1683 * I only want to hit _one_ server so I know the number of requests I'm
1684 * sending in the pipeline.
1686 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1687 memc
->number_of_hosts
= 1;
1689 size_t max_keys
= 20480;
1692 char **keys
= calloc(max_keys
, sizeof(char*));
1693 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1695 /* First add all of the items.. */
1696 char blob
[1024] = {0};
1697 memcached_return_t rc
;
1699 for (size_t x
= 0; x
< max_keys
; ++x
)
1703 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1705 test_true(keys
[x
] != NULL
);
1706 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1707 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1710 /* Try to get all of them with a large multiget */
1712 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1713 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1714 max_keys
, callbacks
, &counter
, 1);
1716 if (rc
== MEMCACHED_SUCCESS
)
1719 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1720 test_true(rc
== MEMCACHED_END
);
1722 /* Verify that we got all of the items */
1723 test_true(counter
== max_keys
);
1725 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1727 test_true(counter
== 0);
1731 test_fail("note: this test functions differently when in binary mode");
1734 /* Release all allocated resources */
1735 for (size_t x
= 0; x
< max_keys
; ++x
)
1742 memc
->number_of_hosts
= number_of_hosts
;
1743 return TEST_SUCCESS
;
1746 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1748 static test_return_t
key_setup(memcached_st
*memc
)
1752 if (pre_binary(memc
) != TEST_SUCCESS
)
1753 return TEST_SKIPPED
;
1755 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1757 return TEST_SUCCESS
;
1760 static test_return_t
key_teardown(memcached_st
*memc
)
1763 pairs_free(global_pairs
);
1765 return TEST_SUCCESS
;
1768 static test_return_t
block_add_regression(memcached_st
*memc
)
1770 /* First add all of the items.. */
1771 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1773 memcached_return_t rc
;
1774 char blob
[1024] = {0};
1776 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1777 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1780 return TEST_SUCCESS
;
1783 static test_return_t
binary_add_regression(memcached_st
*memc
)
1785 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1786 test_return_t rc
= block_add_regression(memc
);
1787 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1791 static test_return_t
get_stats_keys(memcached_st
*memc
)
1795 memcached_stat_st memc_stat
;
1796 memcached_return_t rc
;
1798 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1799 test_true(rc
== MEMCACHED_SUCCESS
);
1800 for (ptr
= stat_list
; *ptr
; ptr
++)
1805 return TEST_SUCCESS
;
1808 static test_return_t
version_string_test(memcached_st
*memc
__attribute__((unused
)))
1810 const char *version_string
;
1812 version_string
= memcached_lib_version();
1814 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1816 return TEST_SUCCESS
;
1819 static test_return_t
get_stats(memcached_st
*memc
)
1823 memcached_return_t rc
;
1824 memcached_stat_st
*memc_stat
;
1826 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1827 test_true(rc
== MEMCACHED_SUCCESS
);
1829 test_true(rc
== MEMCACHED_SUCCESS
);
1830 test_true(memc_stat
);
1832 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1834 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1835 test_true(rc
== MEMCACHED_SUCCESS
);
1836 for (ptr
= stat_list
; *ptr
; ptr
++);
1841 memcached_stat_free(NULL
, memc_stat
);
1843 return TEST_SUCCESS
;
1846 static test_return_t
add_host_test(memcached_st
*memc
)
1849 memcached_server_st
*servers
;
1850 memcached_return_t rc
;
1851 char servername
[]= "0.example.com";
1853 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1855 test_true(1 == memcached_server_list_count(servers
));
1857 for (x
= 2; x
< 20; x
++)
1859 char buffer
[SMALL_STRING_LEN
];
1861 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1862 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1864 test_true(rc
== MEMCACHED_SUCCESS
);
1865 test_true(x
== memcached_server_list_count(servers
));
1868 rc
= memcached_server_push(memc
, servers
);
1869 test_true(rc
== MEMCACHED_SUCCESS
);
1870 rc
= memcached_server_push(memc
, servers
);
1871 test_true(rc
== MEMCACHED_SUCCESS
);
1873 memcached_server_list_free(servers
);
1875 return TEST_SUCCESS
;
1878 static memcached_return_t
clone_test_callback(memcached_st
*parent
__attribute__((unused
)), memcached_st
*memc_clone
__attribute__((unused
)))
1880 return MEMCACHED_SUCCESS
;
1883 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
__attribute__((unused
)))
1885 return MEMCACHED_SUCCESS
;
1888 static test_return_t
callback_test(memcached_st
*memc
)
1890 /* Test User Data */
1894 memcached_return_t rc
;
1896 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1897 test_true(rc
== MEMCACHED_SUCCESS
);
1898 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1899 test_true(*test_ptr
== x
);
1902 /* Test Clone Callback */
1904 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1905 void *clone_cb_ptr
= *(void **)&clone_cb
;
1906 void *temp_function
= NULL
;
1907 memcached_return_t rc
;
1909 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1911 test_true(rc
== MEMCACHED_SUCCESS
);
1912 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1913 test_true(temp_function
== clone_cb_ptr
);
1916 /* Test Cleanup Callback */
1918 memcached_cleanup_fn cleanup_cb
=
1919 (memcached_cleanup_fn
)cleanup_test_callback
;
1920 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1921 void *temp_function
= NULL
;
1922 memcached_return_t rc
;
1924 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1926 test_true(rc
== MEMCACHED_SUCCESS
);
1927 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1928 test_true(temp_function
== cleanup_cb_ptr
);
1931 return TEST_SUCCESS
;
1934 /* We don't test the behavior itself, we test the switches */
1935 static test_return_t
behavior_test(memcached_st
*memc
)
1940 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1941 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1942 test_true(value
== 1);
1944 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1945 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1946 test_true(value
== 1);
1948 set
= MEMCACHED_HASH_MD5
;
1949 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1950 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1951 test_true(value
== MEMCACHED_HASH_MD5
);
1955 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1956 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1957 test_true(value
== 0);
1959 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1960 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1961 test_true(value
== 0);
1963 set
= MEMCACHED_HASH_DEFAULT
;
1964 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1965 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1966 test_true(value
== MEMCACHED_HASH_DEFAULT
);
1968 set
= MEMCACHED_HASH_CRC
;
1969 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1970 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1971 test_true(value
== MEMCACHED_HASH_CRC
);
1973 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
1974 test_true(value
> 0);
1976 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
1977 test_true(value
> 0);
1979 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
1980 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
1981 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
1983 return TEST_SUCCESS
;
1986 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
1988 memcached_return_t rc
;
1992 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
1993 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
1995 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
1997 if (rc
== MEMCACHED_SUCCESS
)
1999 test_true((bool)value
== set
);
2003 test_false((bool)value
== set
);
2006 return TEST_SUCCESS
;
2010 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2012 memcached_return_t rc
;
2016 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2017 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2019 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2021 if (rc
== MEMCACHED_SUCCESS
)
2023 test_true((bool)value
== set
);
2027 test_false((bool)value
== set
);
2030 return TEST_SUCCESS
;
2034 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2036 memcached_return_t rc
;
2040 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2041 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2043 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2045 if (rc
== MEMCACHED_SUCCESS
)
2047 test_true((bool)value
== set
);
2051 test_false((bool)value
== set
);
2054 return TEST_SUCCESS
;
2057 static test_return_t
fetch_all_results(memcached_st
*memc
)
2059 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2060 char return_key
[MEMCACHED_MAX_KEY
];
2061 size_t return_key_length
;
2063 size_t return_value_length
;
2066 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2067 &return_value_length
, &flags
, &rc
)))
2069 test_true(return_value
);
2070 test_true(rc
== MEMCACHED_SUCCESS
);
2074 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2076 return TEST_SUCCESS
;
2079 /* Test case provided by Cal Haldenbrand */
2080 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2082 unsigned int setter
= 1;
2084 unsigned long long total
= 0;
2087 char randomstuff
[6 * 1024];
2088 memcached_return_t rc
;
2090 memset(randomstuff
, 0, 6 * 1024);
2092 /* We just keep looking at the same values over and over */
2095 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2096 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2100 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2104 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2105 memset(randomstuff
, 0, 6 * 1024);
2106 test_true(size
< 6 * 1024); /* Being safe here */
2108 for (j
= 0 ; j
< size
;j
++)
2109 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2112 snprintf(key
, sizeof(key
), "%u", x
);
2113 rc
= memcached_set(memc
, key
, strlen(key
),
2114 randomstuff
, strlen(randomstuff
), 10, 0);
2115 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2116 /* If we fail, lets try again */
2117 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2118 rc
= memcached_set(memc
, key
, strlen(key
),
2119 randomstuff
, strlen(randomstuff
), 10, 0);
2120 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2123 return TEST_SUCCESS
;
2126 /* Test case provided by Cal Haldenbrand */
2127 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2129 unsigned int setter
;
2133 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2134 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2136 setter
= 20 * 1024576;
2137 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2138 setter
= 20 * 1024576;
2139 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2140 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2141 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2143 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2146 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2148 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2149 char buffer
[SMALL_STRING_LEN
];
2154 memset(buffer
, 0, SMALL_STRING_LEN
);
2156 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2157 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2158 &val_len
, &flags
, &rc
);
2159 if (rc
!= MEMCACHED_SUCCESS
)
2161 if (rc
== MEMCACHED_NOTFOUND
)
2175 return TEST_SUCCESS
;
2178 /* Do a large mget() over all the keys we think exist */
2179 #define KEY_COUNT 3000 // * 1024576
2180 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2182 memcached_return_t rc
;
2183 unsigned int setter
;
2186 size_t key_lengths
[KEY_COUNT
];
2189 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2190 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2192 setter
= 20 * 1024576;
2193 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2194 setter
= 20 * 1024576;
2195 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2196 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2197 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2200 keys
= calloc(KEY_COUNT
, sizeof(char *));
2202 for (x
= 0; x
< KEY_COUNT
; x
++)
2206 snprintf(buffer
, 30, "%u", x
);
2207 keys
[x
]= strdup(buffer
);
2208 key_lengths
[x
]= strlen(keys
[x
]);
2211 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2212 test_true(rc
== MEMCACHED_SUCCESS
);
2214 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
2216 for (x
= 0; x
< KEY_COUNT
; x
++)
2220 return TEST_SUCCESS
;
2223 /* Make sure we behave properly if server list has no values */
2224 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2226 memcached_return_t rc
;
2227 const char *keys
[]= {"fudge", "son", "food"};
2228 size_t key_length
[]= {5, 3, 4};
2231 char return_key
[MEMCACHED_MAX_KEY
];
2232 size_t return_key_length
;
2234 size_t return_value_length
;
2236 /* Here we free everything before running a bunch of mget tests */
2237 memcached_servers_reset(memc
);
2240 /* We need to empty the server before continueing test */
2241 rc
= memcached_flush(memc
, 0);
2242 test_true(rc
== MEMCACHED_NO_SERVERS
);
2244 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2245 test_true(rc
== MEMCACHED_NO_SERVERS
);
2247 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2248 &return_value_length
, &flags
, &rc
)) != NULL
)
2250 test_true(return_value
);
2252 test_true(!return_value
);
2253 test_true(return_value_length
== 0);
2254 test_true(rc
== MEMCACHED_NO_SERVERS
);
2256 for (x
= 0; x
< 3; x
++)
2258 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2259 keys
[x
], key_length
[x
],
2260 (time_t)50, (uint32_t)9);
2261 test_true(rc
== MEMCACHED_NO_SERVERS
);
2264 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2265 test_true(rc
== MEMCACHED_NO_SERVERS
);
2268 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2269 &return_value_length
, &flags
, &rc
)))
2271 test_true(return_value
);
2272 test_true(rc
== MEMCACHED_SUCCESS
);
2273 test_true(return_key_length
== return_value_length
);
2274 test_true(!memcmp(return_value
, return_key
, return_value_length
));
2279 return TEST_SUCCESS
;
2282 #define VALUE_SIZE_BUG5 1048064
2283 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2285 memcached_return_t rc
;
2286 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2287 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2288 char return_key
[MEMCACHED_MAX_KEY
];
2289 size_t return_key_length
;
2291 size_t value_length
;
2295 char insert_data
[VALUE_SIZE_BUG5
];
2297 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2298 insert_data
[x
]= (signed char)rand();
2300 memcached_flush(memc
, 0);
2301 value
= memcached_get(memc
, keys
[0], key_length
[0],
2302 &value_length
, &flags
, &rc
);
2303 test_true(value
== NULL
);
2304 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2307 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2308 &value_length
, &flags
, &rc
)))
2310 test_true(count
== 0);
2312 for (x
= 0; x
< 4; x
++)
2314 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2315 insert_data
, VALUE_SIZE_BUG5
,
2316 (time_t)0, (uint32_t)0);
2317 test_true(rc
== MEMCACHED_SUCCESS
);
2320 for (x
= 0; x
< 10; x
++)
2322 value
= memcached_get(memc
, keys
[0], key_length
[0],
2323 &value_length
, &flags
, &rc
);
2327 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2329 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2330 &value_length
, &flags
, &rc
)))
2335 test_true(count
== 4);
2338 return TEST_SUCCESS
;
2341 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2343 memcached_return_t rc
;
2344 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2345 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2346 char return_key
[MEMCACHED_MAX_KEY
];
2347 size_t return_key_length
;
2349 size_t value_length
;
2353 char insert_data
[VALUE_SIZE_BUG5
];
2355 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2356 insert_data
[x
]= (signed char)rand();
2358 memcached_flush(memc
, 0);
2359 value
= memcached_get(memc
, keys
[0], key_length
[0],
2360 &value_length
, &flags
, &rc
);
2361 test_true(value
== NULL
);
2362 test_true(rc
== MEMCACHED_NOTFOUND
);
2363 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2364 test_true(rc
== MEMCACHED_SUCCESS
);
2367 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2368 &value_length
, &flags
, &rc
)))
2370 test_true(count
== 0);
2371 test_true(rc
== MEMCACHED_END
);
2373 for (x
= 0; x
< 4; x
++)
2375 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2376 insert_data
, VALUE_SIZE_BUG5
,
2377 (time_t)0, (uint32_t)0);
2378 test_true(rc
== MEMCACHED_SUCCESS
);
2381 for (x
= 0; x
< 2; x
++)
2383 value
= memcached_get(memc
, keys
[0], key_length
[0],
2384 &value_length
, &flags
, &rc
);
2388 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2389 test_true(rc
== MEMCACHED_SUCCESS
);
2391 /* We test for purge of partial complete fetches */
2392 for (count
= 3; count
; count
--)
2394 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2395 &value_length
, &flags
, &rc
);
2396 test_true(rc
== MEMCACHED_SUCCESS
);
2397 test_true(!(memcmp(value
, insert_data
, value_length
)));
2398 test_true(value_length
);
2403 return TEST_SUCCESS
;
2406 static test_return_t
user_supplied_bug8(memcached_st
*memc
__attribute__((unused
)))
2408 memcached_return_t rc
;
2410 memcached_st
*memc_clone
;
2412 memcached_server_st
*servers
;
2413 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";
2415 servers
= memcached_servers_parse(server_list
);
2418 mine
= memcached_create(NULL
);
2419 rc
= memcached_server_push(mine
, servers
);
2420 test_true(rc
== MEMCACHED_SUCCESS
);
2421 memcached_server_list_free(servers
);
2424 memc_clone
= memcached_clone(NULL
, mine
);
2426 memcached_quit(mine
);
2427 memcached_quit(memc_clone
);
2430 memcached_free(mine
);
2431 memcached_free(memc_clone
);
2433 return TEST_SUCCESS
;
2436 /* Test flag store/retrieve */
2437 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2439 memcached_return_t rc
;
2440 const char *keys
= "036790384900";
2441 size_t key_length
= strlen(keys
);
2442 char return_key
[MEMCACHED_MAX_KEY
];
2443 size_t return_key_length
;
2445 size_t value_length
;
2448 char insert_data
[VALUE_SIZE_BUG5
];
2450 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2451 insert_data
[x
]= (signed char)rand();
2453 memcached_flush(memc
, 0);
2456 rc
= memcached_set(memc
, keys
, key_length
,
2457 insert_data
, VALUE_SIZE_BUG5
,
2459 test_true(rc
== MEMCACHED_SUCCESS
);
2462 value
= memcached_get(memc
, keys
, key_length
,
2463 &value_length
, &flags
, &rc
);
2464 test_true(flags
== 245);
2468 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2471 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2472 &value_length
, &flags
, &rc
);
2473 test_true(flags
== 245);
2478 return TEST_SUCCESS
;
2481 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2483 memcached_return_t rc
;
2484 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2485 size_t key_length
[3];
2490 char return_key
[MEMCACHED_MAX_KEY
];
2491 size_t return_key_length
;
2493 size_t return_value_length
;
2496 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2497 key_length
[1]= strlen("fudge&*@#");
2498 key_length
[2]= strlen("for^#@&$not");
2501 for (x
= 0; x
< 3; x
++)
2503 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2504 keys
[x
], key_length
[x
],
2505 (time_t)50, (uint32_t)9);
2506 test_true(rc
== MEMCACHED_SUCCESS
);
2509 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2510 test_true(rc
== MEMCACHED_SUCCESS
);
2512 /* We need to empty the server before continueing test */
2513 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2514 &return_value_length
, &flags
, &rc
)) != NULL
)
2516 test_true(return_value
);
2520 test_true(count
== 3);
2522 return TEST_SUCCESS
;
2525 /* We are testing with aggressive timeout to get failures */
2526 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2528 const char *key
= "foo";
2530 size_t value_length
= 512;
2533 memcached_return_t rc
;
2534 unsigned int set
= 1;
2535 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2538 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2539 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2541 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2544 value
= (char*)malloc(value_length
* sizeof(char));
2546 for (x
= 0; x
< value_length
; x
++)
2547 value
[x
]= (char) (x
% 127);
2549 for (x
= 1; x
<= 100000; ++x
)
2551 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2553 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2554 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2556 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2561 memcached_free(mclone
);
2563 return TEST_SUCCESS
;
2567 We are looking failures in the async protocol
2569 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2571 const char *key
= "foo";
2573 size_t value_length
= 512;
2576 memcached_return_t rc
;
2577 unsigned int set
= 1;
2579 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2581 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2582 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2584 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2587 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2589 test_true(timeout
== -1);
2591 value
= (char*)malloc(value_length
* sizeof(char));
2593 for (x
= 0; x
< value_length
; x
++)
2594 value
[x
]= (char) (x
% 127);
2596 for (x
= 1; x
<= 100000; ++x
)
2598 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2602 memcached_free(mclone
);
2604 return TEST_SUCCESS
;
2608 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2610 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2612 memcached_return_t rc
;
2614 size_t value_length
;
2616 uint64_t number_value
;
2618 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2619 &value_length
, &flags
, &rc
);
2620 test_true(value
== NULL
);
2621 test_true(rc
== MEMCACHED_NOTFOUND
);
2623 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2626 test_true(value
== NULL
);
2627 /* The binary protocol will set the key if it doesn't exist */
2628 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2630 test_true(rc
== MEMCACHED_SUCCESS
);
2634 test_true(rc
== MEMCACHED_NOTFOUND
);
2637 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2639 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2640 &value_length
, &flags
, &rc
);
2642 test_true(rc
== MEMCACHED_SUCCESS
);
2645 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2647 test_true(number_value
== 2);
2648 test_true(rc
== MEMCACHED_SUCCESS
);
2650 return TEST_SUCCESS
;
2654 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2655 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2657 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2659 char key
[] = "key34567890";
2661 memcached_return_t rc
;
2662 size_t overflowSize
;
2664 char commandFirst
[]= "set key34567890 0 0 ";
2665 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2666 size_t commandLength
;
2669 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2671 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2673 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2675 overflow
= malloc(testSize
);
2676 test_true(overflow
!= NULL
);
2678 memset(overflow
, 'x', testSize
);
2679 rc
= memcached_set(memc
, key
, strlen(key
),
2680 overflow
, testSize
, 0, 0);
2681 test_true(rc
== MEMCACHED_SUCCESS
);
2685 return TEST_SUCCESS
;
2690 Test values of many different sizes
2691 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2692 set key34567890 0 0 8169 \r\n
2693 is sent followed by buffer of size 8169, followed by 8169
2695 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2698 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2699 memcached_return_t rc
;
2700 const char *key
= "foo";
2702 size_t value_length
= 18000;
2704 size_t string_length
;
2707 size_t current_length
;
2709 value
= (char*)malloc(value_length
);
2712 for (x
= 0; x
< value_length
; x
++)
2713 value
[x
] = (char) (x
% 127);
2715 for (current_length
= 0; current_length
< value_length
; current_length
++)
2717 rc
= memcached_set(memc
, key
, strlen(key
),
2718 value
, current_length
,
2719 (time_t)0, (uint32_t)0);
2720 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2722 string
= memcached_get(memc
, key
, strlen(key
),
2723 &string_length
, &flags
, &rc
);
2725 test_true(rc
== MEMCACHED_SUCCESS
);
2726 test_true(string_length
== current_length
);
2727 test_true(!memcmp(string
, value
, string_length
));
2734 return TEST_SUCCESS
;
2738 Look for zero length value problems
2740 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2743 memcached_return_t rc
;
2744 const char *key
= "mykey";
2749 for (x
= 0; x
< 2; x
++)
2751 rc
= memcached_set(memc
, key
, strlen(key
),
2753 (time_t)0, (uint32_t)0);
2755 test_true(rc
== MEMCACHED_SUCCESS
);
2757 value
= memcached_get(memc
, key
, strlen(key
),
2758 &length
, &flags
, &rc
);
2760 test_true(rc
== MEMCACHED_SUCCESS
);
2761 test_true(value
== NULL
);
2762 test_true(length
== 0);
2763 test_true(flags
== 0);
2765 value
= memcached_get(memc
, key
, strlen(key
),
2766 &length
, &flags
, &rc
);
2768 test_true(rc
== MEMCACHED_SUCCESS
);
2769 test_true(value
== NULL
);
2770 test_true(length
== 0);
2771 test_true(flags
== 0);
2774 return TEST_SUCCESS
;
2777 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2778 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2780 memcached_return_t rc
;
2781 const char *key
= "mykey";
2786 rc
= memcached_set(memc
, key
, strlen(key
),
2788 (time_t)0, UINT32_MAX
);
2790 test_true(rc
== MEMCACHED_SUCCESS
);
2792 value
= memcached_get(memc
, key
, strlen(key
),
2793 &length
, &flags
, &rc
);
2795 test_true(rc
== MEMCACHED_SUCCESS
);
2796 test_true(value
== NULL
);
2797 test_true(length
== 0);
2798 test_true(flags
== UINT32_MAX
);
2800 return TEST_SUCCESS
;
2803 #if !defined(__sun) && !defined(__OpenBSD__)
2804 /* Check the validity of chinese key*/
2805 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2807 memcached_return_t rc
;
2808 const char *key
= "豆瓣";
2809 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2814 rc
= memcached_set(memc
, key
, strlen(key
),
2815 value
, strlen(value
),
2818 test_true(rc
== MEMCACHED_SUCCESS
);
2820 value2
= memcached_get(memc
, key
, strlen(key
),
2821 &length
, &flags
, &rc
);
2823 test_true(length
==strlen(value
));
2824 test_true(rc
== MEMCACHED_SUCCESS
);
2825 test_true(memcmp(value
, value2
, length
)==0);
2828 return TEST_SUCCESS
;
2836 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2839 const memcached_server_st
*server
;
2840 memcached_return_t res
;
2844 memc
= memcached_create(NULL
);
2845 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2846 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2848 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2850 memcached_free(memc
);
2852 return TEST_SUCCESS
;
2855 /* CAS test from Andei */
2856 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2858 memcached_return_t status
;
2859 memcached_result_st
*result
, result_obj
;
2860 const char *key
= "abc";
2861 size_t key_len
= strlen("abc");
2862 const char *value
= "foobar";
2863 size_t value_len
= strlen(value
);
2865 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2867 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2868 test_true(status
== MEMCACHED_SUCCESS
);
2870 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2871 test_true(status
== MEMCACHED_SUCCESS
);
2873 result
= memcached_result_create(memc
, &result_obj
);
2876 memcached_result_create(memc
, &result_obj
);
2877 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2880 test_true(status
== MEMCACHED_SUCCESS
);
2882 memcached_result_free(result
);
2884 return TEST_SUCCESS
;
2887 #include "ketama_test_cases.h"
2888 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2890 memcached_return_t rc
;
2893 memcached_server_st
*server_pool
;
2898 memc
= memcached_create(NULL
);
2901 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2902 test_true(rc
== MEMCACHED_SUCCESS
);
2904 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2905 test_true(value
== 1);
2907 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2908 test_true(rc
== MEMCACHED_SUCCESS
);
2910 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2911 test_true(value
== MEMCACHED_HASH_MD5
);
2913 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");
2914 memcached_server_push(memc
, server_pool
);
2916 /* verify that the server list was parsed okay. */
2917 test_true(memcached_server_count(memc
) == 8);
2918 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2919 test_true(server_pool
[0].port
== 11211);
2920 test_true(server_pool
[0].weight
== 600);
2921 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2922 test_true(server_pool
[2].port
== 11211);
2923 test_true(server_pool
[2].weight
== 200);
2924 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2925 test_true(server_pool
[7].port
== 11211);
2926 test_true(server_pool
[7].weight
== 100);
2928 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2929 * us test the boundary wraparound.
2931 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
2933 /* verify the standard ketama set. */
2934 for (x
= 0; x
< 99; x
++)
2936 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2938 memcached_server_instance_st instance
=
2939 memcached_server_instance_by_position(memc
, server_idx
);
2941 const char *hostname
= memcached_server_name(instance
);
2942 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2945 memcached_server_list_free(server_pool
);
2946 memcached_free(memc
);
2948 return TEST_SUCCESS
;
2951 /* Large mget() of missing keys with binary proto
2953 * If many binary quiet commands (such as getq's in an mget) fill the output
2954 * buffer and the server chooses not to respond, memcached_flush hangs. See
2955 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2958 /* sighandler_t function that always asserts false */
2959 static void fail(int unused
__attribute__((unused
)))
2965 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
2970 return TEST_SKIPPED
;
2972 memcached_return_t rc
;
2975 size_t* key_lengths
;
2976 void (*oldalarm
)(int);
2977 memcached_st
*memc_clone
;
2979 memc_clone
= memcached_clone(NULL
, memc
);
2980 test_true(memc_clone
);
2982 /* only binproto uses getq for mget */
2983 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
2985 /* empty the cache to ensure misses (hence non-responses) */
2986 rc
= memcached_flush(memc_clone
, 0);
2987 test_true(rc
== MEMCACHED_SUCCESS
);
2989 key_lengths
= calloc(key_count
, sizeof(size_t));
2990 keys
= calloc(key_count
, sizeof(char *));
2992 for (x
= 0; x
< key_count
; x
++)
2996 snprintf(buffer
, 30, "%u", x
);
2997 keys
[x
]= strdup(buffer
);
2998 key_lengths
[x
]= strlen(keys
[x
]);
3001 oldalarm
= signal(SIGALRM
, fail
);
3004 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3005 test_true(rc
== MEMCACHED_SUCCESS
);
3008 signal(SIGALRM
, oldalarm
);
3010 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3012 for (x
= 0; x
< key_count
; x
++)
3017 memcached_free(memc_clone
);
3019 return TEST_SUCCESS
;
3023 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3025 test_return_t test_rc
;
3026 test_rc
= pre_binary(memc
);
3028 if (test_rc
!= TEST_SUCCESS
)
3033 /* should work as of r580 */
3034 rc
= _user_supplied_bug21(memc
, 10);
3035 test_true(rc
== TEST_SUCCESS
);
3037 /* should fail as of r580 */
3038 rc
= _user_supplied_bug21(memc
, 1000);
3039 test_true(rc
== TEST_SUCCESS
);
3041 return TEST_SUCCESS
;
3044 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3047 memcached_server_instance_st instance
;
3049 memcached_return_t rc
;
3050 memcached_st
*memc
= memcached_create(NULL
);
3053 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3054 test_true(rc
== MEMCACHED_SUCCESS
);
3056 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3057 test_true(value
== 1);
3059 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3060 test_true(rc
== MEMCACHED_SUCCESS
);
3062 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3063 test_true(value
== MEMCACHED_HASH_MD5
);
3065 /* server should be removed when in delay */
3066 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3067 test_true(rc
== MEMCACHED_SUCCESS
);
3069 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3070 test_true(value
== 1);
3072 memcached_server_st
*server_pool
;
3073 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");
3074 memcached_server_push(memc
, server_pool
);
3076 /* verify that the server list was parsed okay. */
3077 test_true(memcached_server_count(memc
) == 8);
3078 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3079 test_true(server_pool
[0].port
== 11211);
3080 test_true(server_pool
[0].weight
== 600);
3081 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3082 test_true(server_pool
[2].port
== 11211);
3083 test_true(server_pool
[2].weight
== 200);
3084 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3085 test_true(server_pool
[7].port
== 11211);
3086 test_true(server_pool
[7].weight
== 100);
3088 instance
= memcached_server_instance_by_position(memc
, 2);
3089 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3090 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3093 This would not work if there were only two hosts.
3095 for (size_t x
= 0; x
< 99; x
++)
3097 memcached_autoeject(memc
);
3098 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3099 test_true(server_idx
!= 2);
3102 /* and re-added when it's back. */
3103 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3104 memc
->next_distribution_rebuild
= time(NULL
) - 1;
3105 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3106 memc
->distribution
);
3107 for (size_t x
= 0; x
< 99; x
++)
3109 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3110 // We re-use instance from above.
3112 memcached_server_instance_by_position(memc
, server_idx
);
3113 const char *hostname
= memcached_server_name(instance
);
3114 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3117 memcached_server_list_free(server_pool
);
3118 memcached_free(memc
);
3120 return TEST_SUCCESS
;
3123 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3127 memcached_return_t rc
;
3128 memcached_st
*memc
= memcached_create(NULL
);
3132 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3133 test_true(rc
== MEMCACHED_SUCCESS
);
3135 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3136 test_true(value
== 1);
3138 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3139 test_true(rc
== MEMCACHED_SUCCESS
);
3141 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3142 test_true(value
== MEMCACHED_HASH_MD5
);
3145 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3147 memcached_server_st
*server_pool
;
3148 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");
3149 memcached_server_push(memc
, server_pool
);
3151 // @todo this needs to be refactored to actually test something.
3154 if ((fp
= fopen("ketama_keys.txt", "w")))
3158 printf("cannot write to file ketama_keys.txt");
3159 return TEST_FAILURE
;
3162 for (int x
= 0; x
< 10000; x
++)
3165 snprintf(key
, sizeof(key
), "%d", x
);
3167 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3168 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3169 in_port_t port
= memc
->hosts
[server_idx
].port
;
3170 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3171 memcached_server_instance_st instance
=
3172 memcached_server_instance_by_position(memc
, host_index
);
3176 memcached_server_list_free(server_pool
);
3177 memcached_free(memc
);
3179 return TEST_SUCCESS
;
3183 static test_return_t
result_static(memcached_st
*memc
)
3185 memcached_result_st result
;
3186 memcached_result_st
*result_ptr
;
3188 result_ptr
= memcached_result_create(memc
, &result
);
3189 test_true(result
.options
.is_allocated
== false);
3190 test_true(memcached_is_initialized(&result
) == true);
3191 test_true(result_ptr
);
3192 test_true(result_ptr
== &result
);
3194 memcached_result_free(&result
);
3196 test_true(result
.options
.is_allocated
== false);
3197 test_true(memcached_is_initialized(&result
) == false);
3199 return TEST_SUCCESS
;
3202 static test_return_t
result_alloc(memcached_st
*memc
)
3204 memcached_result_st
*result_ptr
;
3206 result_ptr
= memcached_result_create(memc
, NULL
);
3207 test_true(result_ptr
);
3208 test_true(result_ptr
->options
.is_allocated
== true);
3209 test_true(memcached_is_initialized(result_ptr
) == true);
3210 memcached_result_free(result_ptr
);
3212 return TEST_SUCCESS
;
3215 static test_return_t
string_static_null(memcached_st
*memc
)
3217 memcached_string_st string
;
3218 memcached_string_st
*string_ptr
;
3220 string_ptr
= memcached_string_create(memc
, &string
, 0);
3221 test_true(string
.options
.is_initialized
== true);
3222 test_true(string_ptr
);
3224 /* The following two better be the same! */
3225 test_true(memcached_is_allocated(string_ptr
) == false);
3226 test_true(memcached_is_allocated(&string
) == false);
3227 test_true(&string
== string_ptr
);
3229 test_true(string
.options
.is_initialized
== true);
3230 test_true(memcached_is_initialized(&string
) == true);
3231 memcached_string_free(&string
);
3232 test_true(memcached_is_initialized(&string
) == false);
3234 return TEST_SUCCESS
;
3237 static test_return_t
string_alloc_null(memcached_st
*memc
)
3239 memcached_string_st
*string
;
3241 string
= memcached_string_create(memc
, NULL
, 0);
3243 test_true(memcached_is_allocated(string
) == true);
3244 test_true(memcached_is_initialized(string
) == true);
3245 memcached_string_free(string
);
3247 return TEST_SUCCESS
;
3250 static test_return_t
string_alloc_with_size(memcached_st
*memc
)
3252 memcached_string_st
*string
;
3254 string
= memcached_string_create(memc
, NULL
, 1024);
3256 test_true(memcached_is_allocated(string
) == true);
3257 test_true(memcached_is_initialized(string
) == true);
3258 memcached_string_free(string
);
3260 return TEST_SUCCESS
;
3263 static test_return_t
string_alloc_with_size_toobig(memcached_st
*memc
)
3265 memcached_string_st
*string
;
3267 string
= memcached_string_create(memc
, NULL
, SIZE_MAX
);
3268 test_true(string
== NULL
);
3270 return TEST_SUCCESS
;
3273 static test_return_t
string_alloc_append(memcached_st
*memc
)
3276 char buffer
[SMALL_STRING_LEN
];
3277 memcached_string_st
*string
;
3279 /* Ring the bell! */
3280 memset(buffer
, 6, SMALL_STRING_LEN
);
3282 string
= memcached_string_create(memc
, NULL
, 100);
3284 test_true(memcached_is_allocated(string
) == true);
3285 test_true(memcached_is_initialized(string
) == true);
3287 for (x
= 0; x
< 1024; x
++)
3289 memcached_return_t rc
;
3290 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3291 test_true(rc
== MEMCACHED_SUCCESS
);
3293 test_true(memcached_is_allocated(string
) == true);
3294 memcached_string_free(string
);
3296 return TEST_SUCCESS
;
3299 static test_return_t
string_alloc_append_toobig(memcached_st
*memc
)
3301 memcached_return_t rc
;
3303 char buffer
[SMALL_STRING_LEN
];
3304 memcached_string_st
*string
;
3306 /* Ring the bell! */
3307 memset(buffer
, 6, SMALL_STRING_LEN
);
3309 string
= memcached_string_create(memc
, NULL
, 100);
3311 test_true(memcached_is_allocated(string
) == true);
3312 test_true(memcached_is_initialized(string
) == true);
3314 for (x
= 0; x
< 1024; x
++)
3316 rc
= memcached_string_append(string
, buffer
, SMALL_STRING_LEN
);
3317 test_true(rc
== MEMCACHED_SUCCESS
);
3319 rc
= memcached_string_append(string
, buffer
, SIZE_MAX
);
3320 test_true(rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
);
3321 test_true(memcached_is_allocated(string
) == true);
3322 memcached_string_free(string
);
3324 return TEST_SUCCESS
;
3327 static test_return_t
cleanup_pairs(memcached_st
*memc
__attribute__((unused
)))
3329 pairs_free(global_pairs
);
3331 return TEST_SUCCESS
;
3334 static test_return_t
generate_pairs(memcached_st
*memc
__attribute__((unused
)))
3336 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3337 global_count
= GLOBAL_COUNT
;
3339 for (size_t x
= 0; x
< global_count
; x
++)
3341 global_keys
[x
]= global_pairs
[x
].key
;
3342 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3345 return TEST_SUCCESS
;
3348 static test_return_t
generate_large_pairs(memcached_st
*memc
__attribute__((unused
)))
3350 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3351 global_count
= GLOBAL2_COUNT
;
3353 for (size_t x
= 0; x
< global_count
; x
++)
3355 global_keys
[x
]= global_pairs
[x
].key
;
3356 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3359 return TEST_SUCCESS
;
3362 static test_return_t
generate_data(memcached_st
*memc
)
3364 execute_set(memc
, global_pairs
, global_count
);
3366 return TEST_SUCCESS
;
3369 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3371 memcached_stat_st
*stat_p
;
3372 memcached_return_t rc
;
3373 uint32_t host_index
= 0;
3374 execute_set(memc
, global_pairs
, global_count
);
3376 //TODO: hosts used size stats
3377 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3380 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3382 /* This test was changes so that "make test" would work properlly */
3384 memcached_server_instance_st instance
=
3385 memcached_server_instance_by_position(memc
, host_index
);
3387 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3389 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3392 memcached_stat_free(NULL
, stat_p
);
3394 return TEST_SUCCESS
;
3396 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3401 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3402 generate_data(memc
);
3404 return TEST_SUCCESS
;
3407 static test_return_t
get_read_count(memcached_st
*memc
)
3409 memcached_return_t rc
;
3410 memcached_st
*memc_clone
;
3412 memc_clone
= memcached_clone(NULL
, memc
);
3413 test_true(memc_clone
);
3415 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3419 size_t return_value_length
;
3423 for (size_t x
= count
= 0; x
< global_count
; x
++)
3425 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3426 &return_value_length
, &flags
, &rc
);
3427 if (rc
== MEMCACHED_SUCCESS
)
3436 memcached_free(memc_clone
);
3438 return TEST_SUCCESS
;
3441 static test_return_t
get_read(memcached_st
*memc
)
3443 memcached_return_t rc
;
3447 size_t return_value_length
;
3450 for (size_t x
= 0; x
< global_count
; x
++)
3452 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3453 &return_value_length
, &flags
, &rc
);
3455 test_true(return_value);
3456 test_true(rc == MEMCACHED_SUCCESS);
3458 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3463 return TEST_SUCCESS
;
3466 static test_return_t
mget_read(memcached_st
*memc
)
3468 memcached_return_t rc
;
3470 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3471 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3472 test_true(fetch_all_results(memc
) == TEST_SUCCESS
);
3474 return TEST_SUCCESS
;
3477 static test_return_t
mget_read_result(memcached_st
*memc
)
3479 memcached_return_t rc
;
3481 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3482 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3483 /* Turn this into a help function */
3485 memcached_result_st results_obj
;
3486 memcached_result_st
*results
;
3488 results
= memcached_result_create(memc
, &results_obj
);
3490 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3493 test_true(rc
== MEMCACHED_SUCCESS
);
3496 memcached_result_free(&results_obj
);
3499 return TEST_SUCCESS
;
3502 static test_return_t
mget_read_function(memcached_st
*memc
)
3504 memcached_return_t rc
;
3506 memcached_execute_fn callbacks
[1];
3508 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3509 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3511 callbacks
[0]= &callback_counter
;
3513 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3515 return TEST_SUCCESS
;
3518 static test_return_t
delete_generate(memcached_st
*memc
)
3520 for (size_t x
= 0; x
< global_count
; x
++)
3522 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3525 return TEST_SUCCESS
;
3528 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3533 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3535 for (size_t x
= 0; x
< global_count
; x
++)
3537 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3540 return TEST_SUCCESS
;
3543 static test_return_t
add_host_test1(memcached_st
*memc
)
3545 memcached_return_t rc
;
3546 char servername
[]= "0.example.com";
3547 memcached_server_st
*servers
;
3549 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3551 test_true(1 == memcached_server_list_count(servers
));
3553 for (size_t x
= 2; x
< 20; x
++)
3555 char buffer
[SMALL_STRING_LEN
];
3557 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3558 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3560 test_true(rc
== MEMCACHED_SUCCESS
);
3561 test_true(x
== memcached_server_list_count(servers
));
3564 rc
= memcached_server_push(memc
, servers
);
3565 test_true(rc
== MEMCACHED_SUCCESS
);
3566 rc
= memcached_server_push(memc
, servers
);
3567 test_true(rc
== MEMCACHED_SUCCESS
);
3569 memcached_server_list_free(servers
);
3571 return TEST_SUCCESS
;
3574 static test_return_t
pre_nonblock(memcached_st
*memc
)
3576 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3578 return TEST_SUCCESS
;
3581 static test_return_t
pre_cork(memcached_st
*memc
)
3583 memcached_return_t rc
;
3586 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3589 return TEST_SKIPPED
;
3592 if (rc
== MEMCACHED_SUCCESS
)
3593 return TEST_SUCCESS
;
3595 return TEST_SKIPPED
;
3598 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3605 return TEST_SKIPPED
;
3608 if (rc
!= TEST_SUCCESS
)
3611 return pre_nonblock(memc
);
3614 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3616 memcached_return_t rc
= MEMCACHED_FAILURE
;
3617 memcached_st
*memc_clone
;
3619 memc_clone
= memcached_clone(NULL
, memc
);
3620 test_true(memc_clone
);
3621 // The memcached_version needs to be done on a clone, because the server
3622 // will not toggle protocol on an connection.
3623 memcached_version(memc_clone
);
3625 if (libmemcached_util_version_check(memc_clone
, 1, 3, 0))
3627 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3628 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3629 test_true(rc
== MEMCACHED_SUCCESS
);
3630 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3634 return TEST_SKIPPED
;
3637 memcached_free(memc_clone
);
3639 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3642 static test_return_t
pre_murmur(memcached_st
*memc
)
3644 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3646 return TEST_SUCCESS
;
3649 static test_return_t
pre_jenkins(memcached_st
*memc
)
3651 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3653 return TEST_SUCCESS
;
3657 static test_return_t
pre_md5(memcached_st
*memc
)
3659 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3661 return TEST_SUCCESS
;
3664 static test_return_t
pre_crc(memcached_st
*memc
)
3666 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3668 return TEST_SUCCESS
;
3671 static test_return_t
pre_hsieh(memcached_st
*memc
)
3673 #ifdef HAVE_HSIEH_HASH
3674 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3675 return TEST_SUCCESS
;
3678 return TEST_SKIPPED
;
3682 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3684 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3686 return TEST_SUCCESS
;
3689 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3691 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3693 return TEST_SUCCESS
;
3696 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3698 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3700 return TEST_SUCCESS
;
3703 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3705 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3707 return TEST_SUCCESS
;
3710 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3712 memcached_return_t rc
;
3715 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3716 test_true(rc
== MEMCACHED_SUCCESS
);
3718 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3719 test_true(value
== 1);
3721 return TEST_SUCCESS
;
3724 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3726 memcached_return_t rc
;
3729 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3730 test_true(rc
== MEMCACHED_SUCCESS
);
3732 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3733 test_true(value
== 1);
3735 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3736 test_true(rc
== MEMCACHED_SUCCESS
);
3738 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3739 test_true(value
== MEMCACHED_HASH_MD5
);
3741 return TEST_SUCCESS
;
3745 @note This should be testing to see if the server really supports the binary protocol.
3747 static test_return_t
pre_binary(memcached_st
*memc
)
3749 memcached_return_t rc
= MEMCACHED_FAILURE
;
3751 if (libmemcached_util_version_check(memc
, 1, 3, 0))
3753 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3754 test_true(rc
== MEMCACHED_SUCCESS
);
3755 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3758 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3761 static test_return_t
pre_sasl(memcached_st
*memc
)
3763 memcached_return_t rc
= MEMCACHED_FAILURE
;
3765 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3766 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3767 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3768 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3770 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3772 memcached_server_st
*servers
= memcached_servers_parse(server
);
3773 test_true(servers
!= NULL
);
3774 memcached_servers_reset(memc
);
3775 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3776 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3777 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3778 test_true(rc
== MEMCACHED_SUCCESS
);
3784 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3787 static test_return_t
pre_replication(memcached_st
*memc
)
3789 test_return_t test_rc
;
3790 test_rc
= pre_binary(memc
);
3792 if (test_rc
!= TEST_SUCCESS
)
3796 * Make sure that we store the item on all servers
3797 * (master + replicas == number of servers)
3799 memcached_return_t rc
;
3800 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3801 memcached_server_count(memc
) - 1);
3802 test_true(rc
== MEMCACHED_SUCCESS
);
3803 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3805 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3809 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3813 rc
= pre_replication(memc
);
3814 if (rc
!= TEST_SUCCESS
)
3817 rc
= pre_nonblock(memc
);
3823 static void my_free(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, void *context
)
3826 #ifdef HARD_MALLOC_TESTS
3827 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3835 static void *my_malloc(const memcached_st
*ptr
__attribute__((unused
)), const size_t size
, void *context
)
3838 #ifdef HARD_MALLOC_TESTS
3839 void *ret
= malloc(size
+ 8);
3842 ret
= (void*)((caddr_t
)ret
+ 8);
3845 void *ret
= malloc(size
);
3850 memset(ret
, 0xff, size
);
3857 static void *my_realloc(const memcached_st
*ptr
__attribute__((unused
)), void *mem
, const size_t size
, void *context
)
3860 #ifdef HARD_MALLOC_TESTS
3861 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3862 void *nmem
= realloc(real_ptr
, size
+ 8);
3867 ret
= (void*)((caddr_t
)nmem
+ 8);
3872 return realloc(mem
, size
);
3877 static void *my_calloc(const memcached_st
*ptr
__attribute__((unused
)), size_t nelem
, const size_t size
, void *context
)
3880 #ifdef HARD_MALLOC_TESTS
3881 void *mem
= my_malloc(ptr
, nelem
* size
);
3884 memset(mem
, 0, nelem
* size
);
3889 return calloc(nelem
, size
);
3894 static test_return_t
set_prefix(memcached_st
*memc
)
3896 memcached_return_t rc
;
3897 const char *key
= "mine";
3900 /* Make sure be default none exists */
3901 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3902 test_true(rc
== MEMCACHED_FAILURE
);
3904 /* Test a clean set */
3905 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3906 test_true(rc
== MEMCACHED_SUCCESS
);
3908 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3909 test_true(memcmp(value
, key
, 4) == 0);
3910 test_true(rc
== MEMCACHED_SUCCESS
);
3912 /* Test that we can turn it off */
3913 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3914 test_true(rc
== MEMCACHED_SUCCESS
);
3916 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3917 test_true(rc
== MEMCACHED_FAILURE
);
3919 /* Now setup for main test */
3920 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3921 test_true(rc
== MEMCACHED_SUCCESS
);
3923 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3924 test_true(rc
== MEMCACHED_SUCCESS
);
3925 test_true(memcmp(value
, key
, 4) == 0);
3927 /* Set to Zero, and then Set to something too large */
3930 memset(long_key
, 0, 255);
3932 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3933 test_true(rc
== MEMCACHED_SUCCESS
);
3935 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3936 test_true(rc
== MEMCACHED_FAILURE
);
3937 test_true(value
== NULL
);
3939 /* Test a long key for failure */
3940 /* TODO, extend test to determine based on setting, what result should be */
3941 strcpy(long_key
, "Thisismorethentheallottednumberofcharacters");
3942 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3943 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3944 test_true(rc
== MEMCACHED_SUCCESS
);
3946 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3947 strcpy(long_key
, "This is more then the allotted number of characters");
3948 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3949 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3951 /* Test for a bad prefix, but with a short key */
3952 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3953 test_true(rc
== MEMCACHED_SUCCESS
);
3955 strcpy(long_key
, "dog cat");
3956 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3957 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3960 return TEST_SUCCESS
;
3964 #ifdef MEMCACHED_ENABLE_DEPRECATED
3965 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3967 void *test_ptr
= NULL
;
3970 memcached_malloc_fn malloc_cb
=
3971 (memcached_malloc_fn
)my_malloc
;
3972 cb_ptr
= *(void **)&malloc_cb
;
3973 memcached_return_t rc
;
3975 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3976 test_true(rc
== MEMCACHED_SUCCESS
);
3977 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3978 test_true(rc
== MEMCACHED_SUCCESS
);
3979 test_true(test_ptr
== cb_ptr
);
3983 memcached_realloc_fn realloc_cb
=
3984 (memcached_realloc_fn
)my_realloc
;
3985 cb_ptr
= *(void **)&realloc_cb
;
3986 memcached_return_t rc
;
3988 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3989 test_true(rc
== MEMCACHED_SUCCESS
);
3990 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3991 test_true(rc
== MEMCACHED_SUCCESS
);
3992 test_true(test_ptr
== cb_ptr
);
3996 memcached_free_fn free_cb
=
3997 (memcached_free_fn
)my_free
;
3998 cb_ptr
= *(void **)&free_cb
;
3999 memcached_return_t rc
;
4001 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
4002 test_true(rc
== MEMCACHED_SUCCESS
);
4003 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
4004 test_true(rc
== MEMCACHED_SUCCESS
);
4005 test_true(test_ptr
== cb_ptr
);
4008 return TEST_SUCCESS
;
4013 static test_return_t
set_memory_alloc(memcached_st
*memc
)
4015 memcached_return_t rc
;
4016 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
4017 my_realloc
, my_calloc
, NULL
);
4018 test_true(rc
== MEMCACHED_FAILURE
);
4020 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
4021 my_realloc
, my_calloc
, NULL
);
4023 memcached_malloc_fn mem_malloc
;
4024 memcached_free_fn mem_free
;
4025 memcached_realloc_fn mem_realloc
;
4026 memcached_calloc_fn mem_calloc
;
4027 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4028 &mem_realloc
, &mem_calloc
);
4030 test_true(mem_malloc
== my_malloc
);
4031 test_true(mem_realloc
== my_realloc
);
4032 test_true(mem_calloc
== my_calloc
);
4033 test_true(mem_free
== my_free
);
4035 return TEST_SUCCESS
;
4038 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4041 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4042 memcached_hash_t hash
;
4043 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4044 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4047 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4048 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4050 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4052 if (hash
!= MEMCACHED_HASH_CRC
)
4053 return TEST_SKIPPED
;
4055 return TEST_SUCCESS
;
4058 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4061 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4062 memcached_hash_t hash
;
4063 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4064 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4067 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4068 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4070 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4072 if (hash
!= MEMCACHED_HASH_HSIEH
)
4073 return TEST_SKIPPED
;
4076 return TEST_SUCCESS
;
4079 static test_return_t
enable_cas(memcached_st
*memc
)
4081 unsigned int set
= 1;
4083 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4085 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4087 return TEST_SUCCESS
;
4090 return TEST_SKIPPED
;
4093 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4095 memcached_version(memc
);
4097 memcached_server_instance_st instance
=
4098 memcached_server_instance_by_position(memc
, 0);
4100 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4101 || instance
->minor_version
> 2)
4103 return TEST_SUCCESS
;
4106 return TEST_SKIPPED
;
4109 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4111 memcached_return_t rc
;
4114 memcached_servers_reset(memc
);
4116 if (stat("/tmp/memcached.socket", &buf
))
4117 return TEST_SKIPPED
;
4119 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4121 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4124 static test_return_t
pre_nodelay(memcached_st
*memc
)
4126 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4127 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4129 return TEST_SUCCESS
;
4132 static test_return_t
pre_settimer(memcached_st
*memc
)
4134 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4135 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4137 return TEST_SUCCESS
;
4140 static test_return_t
poll_timeout(memcached_st
*memc
)
4146 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4148 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4150 test_true(timeout
== 100);
4152 return TEST_SUCCESS
;
4155 static test_return_t
noreply_test(memcached_st
*memc
)
4157 memcached_return_t ret
;
4158 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4159 test_true(ret
== MEMCACHED_SUCCESS
);
4160 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4161 test_true(ret
== MEMCACHED_SUCCESS
);
4162 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4163 test_true(ret
== MEMCACHED_SUCCESS
);
4164 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4165 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4166 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4168 for (int count
=0; count
< 5; ++count
)
4170 for (size_t x
= 0; x
< 100; ++x
)
4173 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4174 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4176 size_t len
= (size_t)check_length
;
4181 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4184 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4187 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4190 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4193 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4199 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4203 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4204 ** API and is _ONLY_ done this way to verify that the library works the
4205 ** way it is supposed to do!!!!
4208 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4210 memcached_server_instance_st instance
=
4211 memcached_server_instance_by_position(memc
, x
);
4212 no_msg
+=(int)(instance
->cursor_active
);
4215 test_true(no_msg
== 0);
4216 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4219 ** Now validate that all items was set properly!
4221 for (size_t x
= 0; x
< 100; ++x
)
4225 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4227 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4229 size_t len
= (size_t)check_length
;
4232 char* value
=memcached_get(memc
, key
, strlen(key
),
4233 &length
, &flags
, &ret
);
4234 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4237 case 0: /* FALLTHROUGH */
4238 case 1: /* FALLTHROUGH */
4240 test_true(strncmp(value
, key
, len
) == 0);
4241 test_true(len
== length
);
4244 test_true(length
== len
* 2);
4247 test_true(length
== len
* 3);
4257 /* Try setting an illegal cas value (should not return an error to
4258 * the caller (because we don't expect a return message from the server)
4260 const char* keys
[]= {"0"};
4261 size_t lengths
[]= {1};
4264 memcached_result_st results_obj
;
4265 memcached_result_st
*results
;
4266 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4267 test_true(ret
== MEMCACHED_SUCCESS
);
4269 results
= memcached_result_create(memc
, &results_obj
);
4271 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4273 test_true(ret
== MEMCACHED_SUCCESS
);
4274 uint64_t cas
= memcached_result_cas(results
);
4275 memcached_result_free(&results_obj
);
4277 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4278 test_true(ret
== MEMCACHED_SUCCESS
);
4281 * The item will have a new cas value, so try to set it again with the old
4282 * value. This should fail!
4284 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4285 test_true(ret
== MEMCACHED_SUCCESS
);
4286 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4287 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4288 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4291 return TEST_SUCCESS
;
4294 static test_return_t
analyzer_test(memcached_st
*memc
)
4296 memcached_return_t rc
;
4297 memcached_stat_st
*memc_stat
;
4298 memcached_analysis_st
*report
;
4300 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4301 test_true(rc
== MEMCACHED_SUCCESS
);
4302 test_true(memc_stat
);
4304 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4305 test_true(rc
== MEMCACHED_SUCCESS
);
4309 memcached_stat_free(NULL
, memc_stat
);
4311 return TEST_SUCCESS
;
4314 /* Count the objects */
4315 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
__attribute__((unused
)),
4316 const char *key
__attribute__((unused
)),
4317 size_t key_length
__attribute__((unused
)),
4320 size_t *counter
= (size_t *)context
;
4322 *counter
= *counter
+ 1;
4324 return MEMCACHED_SUCCESS
;
4327 static test_return_t
dump_test(memcached_st
*memc
)
4329 memcached_return_t rc
;
4331 memcached_dump_fn callbacks
[1];
4332 test_return_t main_rc
;
4334 callbacks
[0]= &callback_dump_counter
;
4336 /* No support for Binary protocol yet */
4337 if (memc
->flags
.binary_protocol
)
4338 return TEST_SUCCESS
;
4340 main_rc
= set_test3(memc
);
4342 test_true (main_rc
== TEST_SUCCESS
);
4344 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4345 test_true(rc
== MEMCACHED_SUCCESS
);
4347 /* We may have more then 32 if our previous flush has not completed */
4348 test_true(counter
>= 32);
4350 return TEST_SUCCESS
;
4353 #ifdef HAVE_LIBMEMCACHEDUTIL
4354 static void* connection_release(void *arg
)
4357 memcached_pool_st
* pool
;
4362 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4366 #define POOL_SIZE 10
4367 static test_return_t
connection_pool_test(memcached_st
*memc
)
4369 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4370 test_true(pool
!= NULL
);
4371 memcached_st
*mmc
[POOL_SIZE
];
4372 memcached_return_t rc
;
4374 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4376 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4377 test_true(mmc
[x
] != NULL
);
4378 test_true(rc
== MEMCACHED_SUCCESS
);
4381 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4382 test_true(rc
== MEMCACHED_SUCCESS
);
4386 memcached_pool_st
* pool
;
4388 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4389 pthread_create(&tid
, NULL
, connection_release
, &item
);
4390 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4391 test_true(rc
== MEMCACHED_SUCCESS
);
4392 pthread_join(tid
, NULL
);
4393 test_true(mmc
[9] == item
.mmc
);
4394 const char *key
= "key";
4395 size_t keylen
= strlen(key
);
4397 // verify that I can do ops with all connections
4398 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4399 test_true(rc
== MEMCACHED_SUCCESS
);
4401 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4403 uint64_t number_value
;
4404 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4405 test_true(rc
== MEMCACHED_SUCCESS
);
4406 test_true(number_value
== (x
+1));
4410 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4412 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4416 /* verify that I can set behaviors on the pool when I don't have all
4417 * of the connections in the pool. It should however be enabled
4418 * when I push the item into the pool
4420 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4421 test_true(mmc
[0] != NULL
);
4423 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4424 test_true(rc
== MEMCACHED_SUCCESS
);
4426 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4427 test_true(mmc
[1] != NULL
);
4429 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4430 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4431 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4433 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4434 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4435 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4437 test_true(memcached_pool_destroy(pool
) == memc
);
4439 return TEST_SUCCESS
;
4442 static test_return_t
util_version_test(memcached_st
*memc
)
4446 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4447 test_true(if_successful
== true);
4449 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4451 // We expect failure
4454 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4455 fprintf(stderr
, "\nDumping Server Information\n\n");
4456 memcached_server_fn callbacks
[1];
4458 callbacks
[0]= dump_server_information
;
4459 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4460 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4462 test_true(if_successful
== false);
4464 memcached_server_instance_st instance
=
4465 memcached_server_instance_by_position(memc
, 0);
4467 memcached_version(memc
);
4469 // We only use one binary when we test, so this should be just fine.
4470 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4471 test_true(if_successful
== true);
4473 if (instance
->micro_version
> 0)
4474 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4475 else if (instance
->minor_version
> 0)
4476 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4477 else if (instance
->major_version
> 0)
4478 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4480 test_true(if_successful
== true);
4482 if (instance
->micro_version
> 0)
4483 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4484 else if (instance
->minor_version
> 0)
4485 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4486 else if (instance
->major_version
> 0)
4487 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4489 test_true(if_successful
== false);
4491 return TEST_SUCCESS
;
4494 static test_return_t
ping_test(memcached_st
*memc
)
4496 memcached_return_t rc
;
4497 memcached_server_instance_st instance
=
4498 memcached_server_instance_by_position(memc
, 0);
4500 // Test both the version that returns a code, and the one that does not.
4501 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4502 memcached_server_port(instance
), NULL
));
4504 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4505 memcached_server_port(instance
), &rc
));
4507 test_true(rc
== MEMCACHED_SUCCESS
);
4509 return TEST_SUCCESS
;
4513 static test_return_t
replication_set_test(memcached_st
*memc
)
4515 memcached_return_t rc
;
4516 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4517 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4519 rc
= memcached_set(memc
, "bubba", 5, "0", 1, 0, 0);
4520 test_true(rc
== MEMCACHED_SUCCESS
);
4523 ** We are using the quiet commands to store the replicas, so we need
4524 ** to ensure that all of them are processed before we can continue.
4525 ** In the test we go directly from storing the object to trying to
4526 ** receive the object from all of the different servers, so we
4527 ** could end up in a race condition (the memcached server hasn't yet
4528 ** processed the quiet command from the replication set when it process
4529 ** the request from the other client (created by the clone)). As a
4530 ** workaround for that we call memcached_quit to send the quit command
4531 ** to the server and wait for the response ;-) If you use the test code
4532 ** as an example for your own code, please note that you shouldn't need
4535 memcached_quit(memc
);
4538 ** "bubba" should now be stored on all of our servers. We don't have an
4539 ** easy to use API to address each individual server, so I'll just iterate
4540 ** through a bunch of "master keys" and I should most likely hit all of the
4543 for (int x
= 'a'; x
<= 'z'; ++x
)
4545 char key
[2]= { [0]= (char)x
};
4548 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4550 test_true(rc
== MEMCACHED_SUCCESS
);
4551 test_true(val
!= NULL
);
4555 memcached_free(memc_clone
);
4557 return TEST_SUCCESS
;
4560 static test_return_t
replication_get_test(memcached_st
*memc
)
4562 memcached_return_t rc
;
4565 * Don't do the following in your code. I am abusing the internal details
4566 * within the library, and this is not a supported interface.
4567 * This is to verify correct behavior in the library
4569 for (uint32_t host
= 0; host
< memcached_server_count(memc
); ++host
)
4571 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4572 memcached_server_instance_st instance
=
4573 memcached_server_instance_by_position(memc_clone
, host
);
4575 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4577 for (int x
= 'a'; x
<= 'z'; ++x
)
4579 char key
[2]= { [0]= (char)x
};
4582 char *val
= memcached_get_by_key(memc_clone
, key
, 1, "bubba", 5,
4584 test_true(rc
== MEMCACHED_SUCCESS
);
4585 test_true(val
!= NULL
);
4589 memcached_free(memc_clone
);
4592 return TEST_SUCCESS
;
4595 static test_return_t
replication_mget_test(memcached_st
*memc
)
4597 memcached_return_t rc
;
4598 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4599 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 0);
4601 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4602 size_t len
[]= { 5, 4, 4, 4 };
4604 for (size_t x
= 0; x
< 4; ++x
)
4606 rc
= memcached_set(memc
, keys
[x
], len
[x
], "0", 1, 0, 0);
4607 test_true(rc
== MEMCACHED_SUCCESS
);
4611 ** We are using the quiet commands to store the replicas, so we need
4612 ** to ensure that all of them are processed before we can continue.
4613 ** In the test we go directly from storing the object to trying to
4614 ** receive the object from all of the different servers, so we
4615 ** could end up in a race condition (the memcached server hasn't yet
4616 ** processed the quiet command from the replication set when it process
4617 ** the request from the other client (created by the clone)). As a
4618 ** workaround for that we call memcached_quit to send the quit command
4619 ** to the server and wait for the response ;-) If you use the test code
4620 ** as an example for your own code, please note that you shouldn't need
4623 memcached_quit(memc
);
4626 * Don't do the following in your code. I am abusing the internal details
4627 * within the library, and this is not a supported interface.
4628 * This is to verify correct behavior in the library
4630 memcached_result_st result_obj
;
4631 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; host
++)
4633 memcached_st
*new_clone
= memcached_clone(NULL
, memc
);
4634 memcached_server_instance_st instance
=
4635 memcached_server_instance_by_position(new_clone
, host
);
4636 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4638 for (int x
= 'a'; x
<= 'z'; ++x
)
4640 char key
[2]= { [0]= (char)x
, [1]= 0 };
4642 rc
= memcached_mget_by_key(new_clone
, key
, 1, keys
, len
, 4);
4643 test_true(rc
== MEMCACHED_SUCCESS
);
4645 memcached_result_st
*results
= memcached_result_create(new_clone
, &result_obj
);
4649 while ((results
= memcached_fetch_result(new_clone
, &result_obj
, &rc
)) != NULL
)
4653 test_true(hits
== 4);
4654 memcached_result_free(&result_obj
);
4657 memcached_free(new_clone
);
4660 memcached_free(memc_clone
);
4662 return TEST_SUCCESS
;
4665 static test_return_t
replication_randomize_mget_test(memcached_st
*memc
)
4667 memcached_result_st result_obj
;
4668 memcached_return_t rc
;
4669 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4670 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 3);
4671 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ
, 1);
4673 const char *keys
[]= { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
4674 size_t len
[]= { 4, 4, 4, 4, 4, 4, 4 };
4676 for (size_t x
= 0; x
< 7; ++x
)
4678 rc
= memcached_set(memc
, keys
[x
], len
[x
], "1", 1, 0, 0);
4679 test_true(rc
== MEMCACHED_SUCCESS
);
4682 memcached_quit(memc
);
4684 for (size_t x
= 0; x
< 7; ++x
)
4686 const char key
[2]= { [0]= (const char)x
};
4688 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 7);
4689 test_true(rc
== MEMCACHED_SUCCESS
);
4691 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4695 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4699 test_true(hits
== 7);
4700 memcached_result_free(&result_obj
);
4702 memcached_free(memc_clone
);
4703 return TEST_SUCCESS
;
4706 static test_return_t
replication_delete_test(memcached_st
*memc
)
4708 memcached_return_t rc
;
4709 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
4710 /* Delete the items from all of the servers except 1 */
4711 uint64_t repl
= memcached_behavior_get(memc
,
4712 MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
4713 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, --repl
);
4715 const char *keys
[]= { "bubba", "key1", "key2", "key3" };
4716 size_t len
[]= { 5, 4, 4, 4 };
4718 for (size_t x
= 0; x
< 4; ++x
)
4720 rc
= memcached_delete_by_key(memc
, keys
[0], len
[0], keys
[x
], len
[x
], 0);
4721 test_true(rc
== MEMCACHED_SUCCESS
);
4725 * Don't do the following in your code. I am abusing the internal details
4726 * within the library, and this is not a supported interface.
4727 * This is to verify correct behavior in the library
4729 uint32_t hash
= memcached_generate_hash(memc
, keys
[0], len
[0]);
4730 for (uint32_t x
= 0; x
< (repl
+ 1); ++x
)
4732 memcached_server_instance_st instance
=
4733 memcached_server_instance_by_position(memc_clone
, x
);
4735 ((memcached_server_write_instance_st
)instance
)->port
= 0;
4736 if (++hash
== memc_clone
->number_of_hosts
)
4740 memcached_result_st result_obj
;
4741 for (uint32_t host
= 0; host
< memc_clone
->number_of_hosts
; ++host
)
4743 for (size_t x
= 'a'; x
<= 'z'; ++x
)
4745 const char key
[2]= { [0]= (const char)x
};
4747 rc
= memcached_mget_by_key(memc_clone
, key
, 1, keys
, len
, 4);
4748 test_true(rc
== MEMCACHED_SUCCESS
);
4750 memcached_result_st
*results
= memcached_result_create(memc_clone
, &result_obj
);
4754 while ((results
= memcached_fetch_result(memc_clone
, &result_obj
, &rc
)) != NULL
)
4758 test_true(hits
== 4);
4759 memcached_result_free(&result_obj
);
4762 memcached_free(memc_clone
);
4764 return TEST_SUCCESS
;
4768 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4772 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4773 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4774 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4775 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4776 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4777 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4778 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4779 #ifdef HAVE_HSIEH_HASH
4780 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4782 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4783 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4784 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4786 return TEST_SUCCESS
;
4790 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4792 memcached_return_t expected_rc
= MEMCACHED_FAILURE
;
4793 #ifdef HAVE_HSIEH_HASH
4794 expected_rc
= MEMCACHED_SUCCESS
;
4796 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4797 (uint64_t)MEMCACHED_HASH_HSIEH
);
4798 test_true(rc
== expected_rc
);
4800 return TEST_SUCCESS
;
4803 static test_return_t
one_at_a_time_run (memcached_st
*memc
__attribute__((unused
)))
4808 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4812 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4813 test_true(one_at_a_time_values
[x
] == hash_val
);
4816 return TEST_SUCCESS
;
4819 static test_return_t
md5_run (memcached_st
*memc
__attribute__((unused
)))
4824 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4828 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4829 test_true(md5_values
[x
] == hash_val
);
4832 return TEST_SUCCESS
;
4835 static test_return_t
crc_run (memcached_st
*memc
__attribute__((unused
)))
4840 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4844 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4845 test_true(crc_values
[x
] == hash_val
);
4848 return TEST_SUCCESS
;
4851 static test_return_t
fnv1_64_run (memcached_st
*memc
__attribute__((unused
)))
4856 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4860 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4861 test_true(fnv1_64_values
[x
] == hash_val
);
4864 return TEST_SUCCESS
;
4867 static test_return_t
fnv1a_64_run (memcached_st
*memc
__attribute__((unused
)))
4872 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4876 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4877 test_true(fnv1a_64_values
[x
] == hash_val
);
4880 return TEST_SUCCESS
;
4883 static test_return_t
fnv1_32_run (memcached_st
*memc
__attribute__((unused
)))
4889 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4893 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4894 test_true(fnv1_32_values
[x
] == hash_val
);
4897 return TEST_SUCCESS
;
4900 static test_return_t
fnv1a_32_run (memcached_st
*memc
__attribute__((unused
)))
4905 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4909 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4910 test_true(fnv1a_32_values
[x
] == hash_val
);
4913 return TEST_SUCCESS
;
4916 static test_return_t
hsieh_run (memcached_st
*memc
__attribute__((unused
)))
4921 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4925 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4926 test_true(hsieh_values
[x
] == hash_val
);
4929 return TEST_SUCCESS
;
4932 static test_return_t
murmur_run (memcached_st
*memc
__attribute__((unused
)))
4934 #ifdef WORDS_BIGENDIAN
4935 return TEST_SKIPPED
;
4940 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4944 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4945 test_true(murmur_values
[x
] == hash_val
);
4948 return TEST_SUCCESS
;
4952 static test_return_t
jenkins_run (memcached_st
*memc
__attribute__((unused
)))
4958 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4962 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4963 test_true(jenkins_values
[x
] == hash_val
);
4966 return TEST_SUCCESS
;
4969 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4972 return libhashkit_md5(string
, string_length
);
4975 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4978 return libhashkit_crc32(string
, string_length
);
4981 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4985 const hashkit_st
*kit
;
4987 hashkit_return_t hash_rc
;
4989 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};
4990 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};
4992 kit
= memcached_get_hashkit(memc
);
4994 hashkit_clone(&new_kit
, kit
);
4995 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4996 test_true(hash_rc
== HASHKIT_SUCCESS
);
4998 memcached_set_hashkit(memc
, &new_kit
);
5001 Verify Setting the hash.
5003 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5007 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
5008 test_true(md5_values
[x
] == hash_val
);
5013 Now check memcached_st.
5015 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5019 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5020 test_true(md5_hosts
[x
] == hash_val
);
5023 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
5024 test_true(hash_rc
== HASHKIT_SUCCESS
);
5026 memcached_set_hashkit(memc
, &new_kit
);
5029 Verify Setting the hash.
5031 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5035 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
5036 test_true(crc_values
[x
] == hash_val
);
5039 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
5043 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
5044 test_true(crc_hosts
[x
] == hash_val
);
5047 return TEST_SUCCESS
;
5051 Test case adapted from John Gorman <johngorman2@gmail.com>
5053 We are testing the error condition when we connect to a server via memcached_get()
5054 but find that the server is not available.
5056 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
5059 memcached_st
*tl_memc_h
;
5060 memcached_server_st
*servers
;
5062 const char *key
= "MemcachedLives";
5065 memcached_return rc
;
5069 tl_memc_h
= memcached_create(NULL
);
5070 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5071 memcached_server_push(tl_memc_h
, servers
);
5072 memcached_server_list_free(servers
);
5074 // See if memcached is reachable.
5075 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
5078 test_true(len
== 0);
5079 test_false(rc
== MEMCACHED_SUCCESS
);
5081 memcached_free(tl_memc_h
);
5083 return TEST_SUCCESS
;
5087 We connect to a server which exists, but search for a key that does not exist.
5089 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5091 const char *key
= "MemcachedKeyNotEXIST";
5094 memcached_return rc
;
5097 // See if memcached is reachable.
5098 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
5101 test_true(len
== 0);
5102 test_true(rc
== MEMCACHED_NOTFOUND
);
5104 return TEST_SUCCESS
;
5108 Test case adapted from John Gorman <johngorman2@gmail.com>
5110 We are testing the error condition when we connect to a server via memcached_get_by_key()
5111 but find that the server is not available.
5113 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
5116 memcached_st
*tl_memc_h
;
5117 memcached_server_st
*servers
;
5119 const char *key
= "MemcachedLives";
5122 memcached_return rc
;
5126 tl_memc_h
= memcached_create(NULL
);
5127 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
5128 memcached_server_push(tl_memc_h
, servers
);
5129 memcached_server_list_free(servers
);
5131 // See if memcached is reachable.
5132 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5135 test_true(len
== 0);
5136 test_false(rc
== MEMCACHED_SUCCESS
);
5138 memcached_free(tl_memc_h
);
5140 return TEST_SUCCESS
;
5144 We connect to a server which exists, but search for a key that does not exist.
5146 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
5148 const char *key
= "MemcachedKeyNotEXIST";
5151 memcached_return rc
;
5154 // See if memcached is reachable.
5155 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
5158 test_true(len
== 0);
5159 test_true(rc
== MEMCACHED_NOTFOUND
);
5161 return TEST_SUCCESS
;
5165 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
5167 memcached_return_t rc
;
5170 memcached_server_st
*server_pool
;
5175 memc
= memcached_create(NULL
);
5178 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5179 test_true(rc
== MEMCACHED_SUCCESS
);
5181 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5182 test_true(value
== 1);
5184 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
5185 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
5188 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");
5189 memcached_server_push(memc
, server_pool
);
5191 /* verify that the server list was parsed okay. */
5192 test_true(memcached_server_count(memc
) == 8);
5193 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5194 test_true(server_pool
[0].port
== 11211);
5195 test_true(server_pool
[0].weight
== 600);
5196 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5197 test_true(server_pool
[2].port
== 11211);
5198 test_true(server_pool
[2].weight
== 200);
5199 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5200 test_true(server_pool
[7].port
== 11211);
5201 test_true(server_pool
[7].weight
== 100);
5203 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5204 * us test the boundary wraparound.
5206 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5208 /* verify the standard ketama set. */
5209 for (x
= 0; x
< 99; x
++)
5211 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
5212 memcached_server_instance_st instance
=
5213 memcached_server_instance_by_position(memc
, server_idx
);
5214 const char *hostname
= memcached_server_name(instance
);
5216 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
5219 memcached_server_list_free(server_pool
);
5220 memcached_free(memc
);
5222 return TEST_SUCCESS
;
5225 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
5227 memcached_return_t rc
;
5230 memcached_server_st
*server_pool
;
5235 memc
= memcached_create(NULL
);
5238 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
5239 test_true(rc
== MEMCACHED_SUCCESS
);
5241 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
5242 test_true(value
== 1);
5244 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
5245 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
5247 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");
5248 memcached_server_push(memc
, server_pool
);
5250 /* verify that the server list was parsed okay. */
5251 test_true(memcached_server_count(memc
) == 8);
5252 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5253 test_true(server_pool
[0].port
== 11211);
5254 test_true(server_pool
[0].weight
== 600);
5255 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5256 test_true(server_pool
[2].port
== 11211);
5257 test_true(server_pool
[2].weight
== 200);
5258 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5259 test_true(server_pool
[7].port
== 11211);
5260 test_true(server_pool
[7].weight
== 100);
5262 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5263 * us test the boundary wraparound.
5265 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->continuum
[0].index
);
5267 /* verify the standard ketama set. */
5268 for (x
= 0; x
< 99; x
++)
5270 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5272 memcached_server_instance_st instance
=
5273 memcached_server_instance_by_position(memc
, server_idx
);
5275 const char *hostname
= memcached_server_name(instance
);
5277 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5280 memcached_server_list_free(server_pool
);
5281 memcached_free(memc
);
5283 return TEST_SUCCESS
;
5286 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5288 test_return_t test_rc
;
5289 test_rc
= pre_binary(memc
);
5291 if (test_rc
!= TEST_SUCCESS
)
5294 memcached_return_t ret
;
5295 const char *key
= "regression_bug_434484";
5296 size_t keylen
= strlen(key
);
5298 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5299 test_true(ret
== MEMCACHED_NOTSTORED
);
5301 size_t size
= 2048 * 1024;
5302 void *data
= calloc(1, size
);
5303 test_true(data
!= NULL
);
5304 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5305 test_true(ret
== MEMCACHED_E2BIG
);
5308 return TEST_SUCCESS
;
5311 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5313 test_return_t test_rc
;
5314 test_rc
= pre_binary(memc
);
5316 if (test_rc
!= TEST_SUCCESS
)
5319 memcached_return_t rc
;
5321 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5324 * I only want to hit only _one_ server so I know the number of requests I'm
5325 * sending in the pipleine to the server. Let's try to do a multiget of
5326 * 1024 (that should satisfy most users don't you think?). Future versions
5327 * will include a mget_execute function call if you need a higher number.
5329 uint32_t number_of_hosts
= memcached_server_count(memc
);
5330 memc
->number_of_hosts
= 1;
5331 const size_t max_keys
= 1024;
5332 char **keys
= calloc(max_keys
, sizeof(char*));
5333 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5335 for (size_t x
= 0; x
< max_keys
; ++x
)
5339 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5341 test_true(keys
[x
] != NULL
);
5345 * Run two times.. the first time we should have 100% cache miss,
5346 * and the second time we should have 100% cache hits
5348 for (size_t y
= 0; y
< 2; y
++)
5350 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5351 test_true(rc
== MEMCACHED_SUCCESS
);
5352 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5356 /* The first iteration should give me a 100% cache miss. verify that*/
5357 char blob
[1024]= { 0 };
5359 test_true(counter
== 0);
5361 for (size_t x
= 0; x
< max_keys
; ++x
)
5363 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5364 blob
, sizeof(blob
), 0, 0);
5365 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5370 /* Verify that we received all of the key/value pairs */
5371 test_true(counter
== max_keys
);
5375 /* Release allocated resources */
5376 for (size_t x
= 0; x
< max_keys
; ++x
)
5383 memc
->number_of_hosts
= number_of_hosts
;
5385 return TEST_SUCCESS
;
5388 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5390 memcached_return_t rc
;
5391 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5392 test_true(rc
== MEMCACHED_SUCCESS
);
5394 return regression_bug_434843(memc
);
5397 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5399 memcached_return_t rc
;
5400 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5401 test_true(rc
== MEMCACHED_SUCCESS
);
5403 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5404 test_true(rc
== MEMCACHED_SUCCESS
);
5405 test_true(bytes
!= NULL
);
5406 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5408 test_true(rc
== MEMCACHED_SUCCESS
);
5409 test_true(bytes_read
!= NULL
);
5411 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5412 "bytes_written", &rc
);
5413 test_true(rc
== MEMCACHED_SUCCESS
);
5414 test_true(bytes_written
!= NULL
);
5416 test_true(strcmp(bytes
, bytes_read
) != 0);
5417 test_true(strcmp(bytes
, bytes_written
) != 0);
5419 /* Release allocated resources */
5422 free(bytes_written
);
5423 memcached_stat_free(NULL
, memc_stat
);
5425 return TEST_SUCCESS
;
5429 * The test case isn't obvious so I should probably document why
5430 * it works the way it does. Bug 442914 was caused by a bug
5431 * in the logic in memcached_purge (it did not handle the case
5432 * where the number of bytes sent was equal to the watermark).
5433 * In this test case, create messages so that we hit that case
5434 * and then disable noreply mode and issue a new command to
5435 * verify that it isn't stuck. If we change the format for the
5436 * delete command or the watermarks, we need to update this
5439 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5441 memcached_return_t rc
;
5442 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5443 test_true(rc
== MEMCACHED_SUCCESS
);
5444 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5446 uint32_t number_of_hosts
= memcached_server_count(memc
);
5447 memc
->number_of_hosts
= 1;
5452 for (uint32_t x
= 0; x
< 250; ++x
)
5454 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5455 rc
= memcached_delete(memc
, k
, len
, 0);
5456 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5459 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5462 rc
= memcached_delete(memc
, k
, len
, 0);
5463 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5465 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5466 test_true(rc
== MEMCACHED_SUCCESS
);
5467 rc
= memcached_delete(memc
, k
, len
, 0);
5468 test_true(rc
== MEMCACHED_NOTFOUND
);
5470 memc
->number_of_hosts
= number_of_hosts
;
5472 return TEST_SUCCESS
;
5475 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5477 memcached_server_instance_st instance_one
;
5478 memcached_server_instance_st instance_two
;
5480 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5481 return TEST_SKIPPED
;
5483 memcached_return_t rc
;
5485 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5486 test_true(rc
== MEMCACHED_SUCCESS
);
5488 const size_t max_keys
= 100;
5489 char **keys
= calloc(max_keys
, sizeof(char*));
5490 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5492 for (size_t x
= 0; x
< max_keys
; ++x
)
5496 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5498 test_true(keys
[x
] != NULL
);
5499 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5500 test_true(rc
== MEMCACHED_SUCCESS
);
5504 ** We are using the quiet commands to store the replicas, so we need
5505 ** to ensure that all of them are processed before we can continue.
5506 ** In the test we go directly from storing the object to trying to
5507 ** receive the object from all of the different servers, so we
5508 ** could end up in a race condition (the memcached server hasn't yet
5509 ** processed the quiet command from the replication set when it process
5510 ** the request from the other client (created by the clone)). As a
5511 ** workaround for that we call memcached_quit to send the quit command
5512 ** to the server and wait for the response ;-) If you use the test code
5513 ** as an example for your own code, please note that you shouldn't need
5516 memcached_quit(memc
);
5518 /* Verify that all messages are stored, and we didn't stuff too much
5521 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5522 test_true(rc
== MEMCACHED_SUCCESS
);
5525 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5526 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5527 /* Verify that we received all of the key/value pairs */
5528 test_true(counter
== max_keys
);
5530 memcached_quit(memc
);
5532 * Don't do the following in your code. I am abusing the internal details
5533 * within the library, and this is not a supported interface.
5534 * This is to verify correct behavior in the library. Fake that two servers
5537 instance_one
= memcached_server_instance_by_position(memc
, 0);
5538 instance_two
= memcached_server_instance_by_position(memc
, 2);
5539 in_port_t port0
= instance_one
->port
;
5540 in_port_t port2
= instance_two
->port
;
5542 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5543 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5545 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5546 test_true(rc
== MEMCACHED_SUCCESS
);
5549 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5550 test_true(counter
== (unsigned int)max_keys
);
5552 /* restore the memc handle */
5553 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5554 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5556 memcached_quit(memc
);
5558 /* Remove half of the objects */
5559 for (size_t x
= 0; x
< max_keys
; ++x
)
5563 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5564 test_true(rc
== MEMCACHED_SUCCESS
);
5568 memcached_quit(memc
);
5569 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5570 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5572 /* now retry the command, this time we should have cache misses */
5573 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5574 test_true(rc
== MEMCACHED_SUCCESS
);
5577 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5578 test_true(counter
== (unsigned int)(max_keys
>> 1));
5580 /* Release allocated resources */
5581 for (size_t x
= 0; x
< max_keys
; ++x
)
5588 /* restore the memc handle */
5589 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5590 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5592 return TEST_SUCCESS
;
5595 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5597 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5598 test_true(memc_clone
!= NULL
);
5599 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5601 memcached_server_instance_st instance
=
5602 memcached_server_instance_by_position(memc_clone
, 0);
5604 if (instance
->major_version
> 1 ||
5605 (instance
->major_version
== 1 &&
5606 instance
->minor_version
> 2))
5608 /* Binary protocol doesn't support deferred delete */
5609 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5610 test_true(bin_clone
!= NULL
);
5611 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5612 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5613 memcached_free(bin_clone
);
5615 memcached_quit(memc_clone
);
5617 /* If we know the server version, deferred delete should fail
5618 * with invalid arguments */
5619 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5621 /* If we don't know the server version, we should get a protocol error */
5622 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5624 /* but there is a bug in some of the memcached servers (1.4) that treats
5625 * the counter as noreply so it doesn't send the proper error message
5627 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5629 /* And buffered mode should be disabled and we should get protocol error */
5630 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5631 rc
= memcached_delete(memc
, "foo", 3, 1);
5632 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5634 /* Same goes for noreply... */
5635 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5636 rc
= memcached_delete(memc
, "foo", 3, 1);
5637 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5639 /* but a normal request should go through (and be buffered) */
5640 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5641 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5643 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5644 /* unbuffered noreply should be success */
5645 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5646 /* unbuffered with reply should be not found... */
5647 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5648 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5651 memcached_free(memc_clone
);
5652 return TEST_SUCCESS
;
5656 /* Test memcached_server_get_last_disconnect
5657 * For a working server set, shall be NULL
5658 * For a set of non existing server, shall not be NULL
5660 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5662 memcached_return_t rc
;
5663 memcached_server_instance_st disconnected_server
;
5665 /* With the working set of server */
5666 const char *key
= "marmotte";
5667 const char *value
= "milka";
5669 memcached_reset_last_disconnected_server(memc
);
5670 rc
= memcached_set(memc
, key
, strlen(key
),
5671 value
, strlen(value
),
5672 (time_t)0, (uint32_t)0);
5673 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5675 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5676 test_true(disconnected_server
== NULL
);
5678 /* With a non existing server */
5680 memcached_server_st
*servers
;
5682 const char *server_list
= "localhost:9";
5684 servers
= memcached_servers_parse(server_list
);
5686 mine
= memcached_create(NULL
);
5687 rc
= memcached_server_push(mine
, servers
);
5688 test_true(rc
== MEMCACHED_SUCCESS
);
5689 memcached_server_list_free(servers
);
5692 rc
= memcached_set(mine
, key
, strlen(key
),
5693 value
, strlen(value
),
5694 (time_t)0, (uint32_t)0);
5695 test_true(rc
!= MEMCACHED_SUCCESS
);
5697 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5698 if (disconnected_server
== NULL
)
5700 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5703 test_true(disconnected_server
!= NULL
);
5704 test_true(memcached_server_port(disconnected_server
)== 9);
5705 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5707 memcached_quit(mine
);
5708 memcached_free(mine
);
5710 return TEST_SUCCESS
;
5713 static test_return_t
test_verbosity(memcached_st
*memc
)
5715 memcached_verbosity(memc
, 3);
5717 return TEST_SUCCESS
;
5720 static test_return_t
test_server_failure(memcached_st
*memc
)
5722 memcached_st
*local_memc
;
5723 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5725 local_memc
= memcached_create(NULL
);
5727 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5728 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5730 uint32_t server_count
= memcached_server_count(local_memc
);
5732 test_true(server_count
== 1);
5734 // Disable the server
5735 instance
= memcached_server_instance_by_position(local_memc
, 0);
5736 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5738 memcached_return_t rc
;
5739 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5741 (time_t)0, (uint32_t)0);
5742 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5744 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5745 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5747 (time_t)0, (uint32_t)0);
5748 test_true(rc
== MEMCACHED_SUCCESS
);
5751 memcached_free(local_memc
);
5753 return TEST_SUCCESS
;
5756 static test_return_t
test_cull_servers(memcached_st
*memc
)
5758 uint32_t count
= memcached_server_count(memc
);
5760 // Do not do this in your code, it is not supported.
5761 memc
->servers
[1].state
.is_dead
= true;
5762 memc
->state
.is_time_for_rebuild
= true;
5764 uint32_t new_count
= memcached_server_count(memc
);
5765 test_true(count
== new_count
);
5768 test_true(count
== new_count
+ 1 );
5771 return TEST_SUCCESS
;
5775 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5776 const char *key
, size_t key_length
,
5777 const char *value
, size_t value_length
,
5787 return MEMCACHED_SUCCESS
;
5790 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5792 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5793 test_true(rc
== MEMCACHED_SUCCESS
);
5795 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5796 test_true(rc
== MEMCACHED_SUCCESS
);
5798 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5799 test_true(rc
== MEMCACHED_SUCCESS
);
5801 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5802 test_true(rc
== MEMCACHED_SUCCESS
);
5804 return TEST_SUCCESS
;
5808 * This test ensures that the failure counter isn't incremented during
5809 * normal termination of the memcached instance.
5811 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5813 memcached_return_t rc
;
5814 memcached_server_instance_st instance
;
5816 /* Set value to force connection to the server */
5817 const char *key
= "marmotte";
5818 const char *value
= "milka";
5821 * Please note that I'm abusing the internal structures in libmemcached
5822 * in a non-portable way and you shouldn't be doing this. I'm only
5823 * doing this in order to verify that the library works the way it should
5825 uint32_t number_of_hosts
= memcached_server_count(memc
);
5826 memc
->number_of_hosts
= 1;
5828 /* Ensure that we are connected to the server by setting a value */
5829 rc
= memcached_set(memc
, key
, strlen(key
),
5830 value
, strlen(value
),
5831 (time_t)0, (uint32_t)0);
5832 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5835 instance
= memcached_server_instance_by_position(memc
, 0);
5836 /* The test is to see that the memcached_quit doesn't increase the
5837 * the server failure conter, so let's ensure that it is zero
5838 * before sending quit
5840 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5842 memcached_quit(memc
);
5844 /* Verify that it memcached_quit didn't increment the failure counter
5845 * Please note that this isn't bullet proof, because an error could
5848 test_true(instance
->server_failure_counter
== 0);
5850 /* restore the instance */
5851 memc
->number_of_hosts
= number_of_hosts
;
5853 return TEST_SUCCESS
;
5860 * Test that ensures mget_execute does not end into recursive calls that finally fails
5862 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5864 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5865 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5866 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5867 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5868 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5871 return TEST_SKIPPED
; // My MAC can't handle this test
5875 * I only want to hit _one_ server so I know the number of requests I'm
5876 * sending in the pipeline.
5878 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5879 memc
->number_of_hosts
= 1;
5880 size_t max_keys
= 20480;
5883 char **keys
= calloc(max_keys
, sizeof(char*));
5884 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5886 /* First add all of the items.. */
5888 char blob
[1024]= { 0 };
5889 memcached_return rc
;
5890 for (size_t x
= 0; x
< max_keys
; ++x
)
5893 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5895 assert(keys
[x
] != NULL
);
5896 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5898 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5900 break; // We are out of business
5903 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5905 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5908 sleep(1);// We will try to sleep
5911 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5913 // We failed to send everything.
5918 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5921 /* Try to get all of them with a large multiget */
5923 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5924 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5925 (size_t)max_keys
, callbacks
, &counter
, 1);
5927 assert(rc
== MEMCACHED_SUCCESS
);
5928 char* the_value
= NULL
;
5929 char the_key
[MEMCACHED_MAX_KEY
];
5930 size_t the_key_length
;
5931 size_t the_value_length
;
5935 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5937 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5943 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5946 assert(rc
== MEMCACHED_END
);
5948 /* Verify that we got all of the items */
5949 assert(counter
== max_keys
);
5952 /* Release all allocated resources */
5953 for (size_t x
= 0; x
< max_keys
; ++x
)
5960 memc
->number_of_hosts
= number_of_hosts
;
5962 return TEST_SUCCESS
;
5965 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5969 memcached_st
*memc
= memcached_create(NULL
);
5971 memcached_server_add(memc
, "10.2.3.4", 11211);
5973 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5974 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5975 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5976 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5977 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5978 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5980 memcached_return_t rc
;
5984 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5986 test_true(rc
== MEMCACHED_TIMEOUT
);
5988 memcached_free(memc
);
5990 return TEST_SUCCESS
;
5993 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5995 fprintf(stderr
, "Iteration #%u: ", it
);
5997 if(error
== MEMCACHED_ERRNO
)
5999 fprintf(stderr
, "system error %d from %s: %s\n",
6000 errno
, what
, strerror(errno
));
6004 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
6005 memcached_strerror(mc
, error
));
6009 #define TEST_CONSTANT_CREATION 200
6011 static test_return_t
regression_bug_(memcached_st
*memc
)
6013 const char *remote_server
;
6016 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
6018 return TEST_SKIPPED
;
6021 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
6023 memcached_st
* mc
= memcached_create(NULL
);
6024 memcached_return rc
;
6026 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
6027 if (rc
!= MEMCACHED_SUCCESS
)
6029 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
6032 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
6033 if (rc
!= MEMCACHED_SUCCESS
)
6035 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
6038 rc
= memcached_server_add(mc
, remote_server
, 0);
6039 if (rc
!= MEMCACHED_SUCCESS
)
6041 memcached_die(mc
, rc
, "memcached_server_add", x
);
6044 const char *set_key
= "akey";
6045 const size_t set_key_len
= strlen(set_key
);
6046 const char *set_value
= "a value";
6047 const size_t set_value_len
= strlen(set_value
);
6049 if (rc
== MEMCACHED_SUCCESS
)
6053 size_t get_value_len
;
6055 uint32_t get_value_flags
;
6057 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
6058 &get_value_flags
, &rc
);
6059 if (rc
!= MEMCACHED_SUCCESS
)
6061 memcached_die(mc
, rc
, "memcached_get", x
);
6067 (get_value_len
!= set_value_len
6068 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
6070 fprintf(stderr
, "Values don't match?\n");
6071 rc
= MEMCACHED_FAILURE
;
6077 rc
= memcached_set(mc
,
6078 set_key
, set_key_len
,
6079 set_value
, set_value_len
,
6083 if (rc
!= MEMCACHED_SUCCESS
)
6085 memcached_die(mc
, rc
, "memcached_set", x
);
6092 if (rc
!= MEMCACHED_SUCCESS
)
6098 return TEST_SUCCESS
;
6102 * Test that the sasl authentication works. We cannot use the default
6103 * pool of servers, because that would require that all servers we want
6104 * to test supports SASL authentication, and that they use the default
6107 static test_return_t
sasl_auth_test(memcached_st
*memc
)
6109 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
6110 memcached_return_t rc
;
6112 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6113 test_true(rc
== MEMCACHED_SUCCESS
);
6114 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
6115 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
6116 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
6117 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
6118 memcached_quit(memc
);
6120 rc
= memcached_set_sasl_auth_data(memc
,
6121 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
6122 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
6123 test_true(rc
== MEMCACHED_SUCCESS
);
6125 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
6126 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
6127 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
6129 memcached_quit(memc
);
6130 return TEST_SUCCESS
;
6133 return TEST_FAILURE
;
6137 /* Clean the server before beginning testing */
6139 {"util_version", 1, (test_callback_fn
)util_version_test
},
6140 {"flush", 0, (test_callback_fn
)flush_test
},
6141 {"init", 0, (test_callback_fn
)init_test
},
6142 {"allocation", 0, (test_callback_fn
)allocation_test
},
6143 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
6144 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
6145 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
6146 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
6147 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
6148 {"clone_test", 0, (test_callback_fn
)clone_test
},
6149 {"connection_test", 0, (test_callback_fn
)connection_test
},
6150 {"callback_test", 0, (test_callback_fn
)callback_test
},
6151 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
6152 {"error", 0, (test_callback_fn
)error_test
},
6153 {"set", 0, (test_callback_fn
)set_test
},
6154 {"set2", 0, (test_callback_fn
)set_test2
},
6155 {"set3", 0, (test_callback_fn
)set_test3
},
6156 {"dump", 1, (test_callback_fn
)dump_test
},
6157 {"add", 1, (test_callback_fn
)add_test
},
6158 {"replace", 1, (test_callback_fn
)replace_test
},
6159 {"delete", 1, (test_callback_fn
)delete_test
},
6160 {"get", 1, (test_callback_fn
)get_test
},
6161 {"get2", 0, (test_callback_fn
)get_test2
},
6162 {"get3", 0, (test_callback_fn
)get_test3
},
6163 {"get4", 0, (test_callback_fn
)get_test4
},
6164 {"partial mget", 0, (test_callback_fn
)get_test5
},
6165 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
6166 {"increment", 0, (test_callback_fn
)increment_test
},
6167 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
6168 {"decrement", 0, (test_callback_fn
)decrement_test
},
6169 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
6170 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
6171 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
6172 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
6173 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
6174 {"quit", 0, (test_callback_fn
)quit_test
},
6175 {"mget", 1, (test_callback_fn
)mget_test
},
6176 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
6177 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
6178 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
6179 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
6180 {"mget_end", 0, (test_callback_fn
)mget_end
},
6181 {"get_stats", 0, (test_callback_fn
)get_stats
},
6182 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
6183 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
6184 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
6185 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
6186 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6187 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6188 {"read_through", 1, (test_callback_fn
)read_through
},
6189 {"delete_through", 1, (test_callback_fn
)delete_through
},
6190 {"noreply", 1, (test_callback_fn
)noreply_test
},
6191 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6192 #ifdef HAVE_LIBMEMCACHEDUTIL
6193 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6194 {"ping", 1, (test_callback_fn
)ping_test
},
6196 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6197 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6198 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6199 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6200 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6204 test_st behavior_tests
[] ={
6205 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6206 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6207 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6208 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6212 test_st regression_binary_vs_block
[] ={
6213 {"block add", 1, (test_callback_fn
)block_add_regression
},
6214 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6218 test_st async_tests
[] ={
6219 {"add", 1, (test_callback_fn
)add_wrapper
},
6223 test_st string_tests
[] ={
6224 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6225 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6226 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6227 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6228 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6229 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6230 {0, 0, (test_callback_fn
)0}
6233 test_st result_tests
[] ={
6234 {"result static", 0, (test_callback_fn
)result_static
},
6235 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6236 {0, 0, (test_callback_fn
)0}
6239 test_st version_1_2_3
[] ={
6240 {"append", 0, (test_callback_fn
)append_test
},
6241 {"prepend", 0, (test_callback_fn
)prepend_test
},
6242 {"cas", 0, (test_callback_fn
)cas_test
},
6243 {"cas2", 0, (test_callback_fn
)cas2_test
},
6244 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6245 {0, 0, (test_callback_fn
)0}
6248 test_st user_tests
[] ={
6249 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6250 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6251 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6252 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6253 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6254 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6255 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6256 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6257 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6258 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6259 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6260 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6261 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6262 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6263 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6264 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6265 #if !defined(__sun) && !defined(__OpenBSD__)
6267 ** It seems to be something weird with the character sets..
6268 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6269 ** guess I need to find out how this is supposed to work.. Perhaps I need
6270 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6271 ** so just disable the code for now...).
6273 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6275 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6276 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6277 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6278 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6279 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6280 {0, 0, (test_callback_fn
)0}
6283 test_st replication_tests
[]= {
6284 {"set", 1, (test_callback_fn
)replication_set_test
},
6285 {"get", 0, (test_callback_fn
)replication_get_test
},
6286 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6287 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6288 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6289 {0, 0, (test_callback_fn
)0}
6293 * The following test suite is used to verify that we don't introduce
6294 * regression bugs. If you want more information about the bug / test,
6295 * you should look in the bug report at
6296 * http://bugs.launchpad.net/libmemcached
6298 test_st regression_tests
[]= {
6299 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6300 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6301 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6302 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6303 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6304 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6305 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6306 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6307 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6308 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6309 {0, 0, (test_callback_fn
)0}
6312 test_st sasl_auth_tests
[]= {
6313 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6314 {0, 0, (test_callback_fn
)0}
6317 test_st ketama_compatibility
[]= {
6318 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6319 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6320 {0, 0, (test_callback_fn
)0}
6323 test_st generate_tests
[] ={
6324 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6325 {"generate_data", 1, (test_callback_fn
)generate_data
},
6326 {"get_read", 0, (test_callback_fn
)get_read
},
6327 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6328 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6329 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6330 {"generate_data", 1, (test_callback_fn
)generate_data
},
6331 {"mget_read", 0, (test_callback_fn
)mget_read
},
6332 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6333 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6334 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6335 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6336 {"generate_data", 1, (test_callback_fn
)generate_data
},
6337 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6338 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6339 {0, 0, (test_callback_fn
)0}
6342 test_st consistent_tests
[] ={
6343 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6344 {"generate_data", 1, (test_callback_fn
)generate_data
},
6345 {"get_read", 0, (test_callback_fn
)get_read_count
},
6346 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6347 {0, 0, (test_callback_fn
)0}
6350 test_st consistent_weighted_tests
[] ={
6351 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6352 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6353 {"get_read", 0, (test_callback_fn
)get_read_count
},
6354 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6355 {0, 0, (test_callback_fn
)0}
6358 test_st hsieh_availability
[] ={
6359 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6360 {0, 0, (test_callback_fn
)0}
6364 test_st hash_sanity
[] ={
6365 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6366 {0, 0, (test_callback_fn
)0}
6370 test_st ketama_auto_eject_hosts
[] ={
6371 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6372 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6373 {0, 0, (test_callback_fn
)0}
6376 test_st hash_tests
[] ={
6377 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6378 {"md5", 0, (test_callback_fn
)md5_run
},
6379 {"crc", 0, (test_callback_fn
)crc_run
},
6380 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6381 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6382 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6383 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6384 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6385 {"murmur", 0, (test_callback_fn
)murmur_run
},
6386 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6387 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6388 {0, 0, (test_callback_fn
)0}
6391 test_st error_conditions
[] ={
6392 {"memcached_get_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6393 {"memcached_get_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6394 {"memcached_get_by_key_MEMCACHED_ERRNO", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6395 {"memcached_get_by_key_MEMCACHED_NOTFOUND", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6396 {0, 0, (test_callback_fn
)0}
6399 collection_st collection
[] ={
6401 {"hash_sanity", 0, 0, hash_sanity
},
6403 {"hsieh_availability", 0, 0, hsieh_availability
},
6404 {"block", 0, 0, tests
},
6405 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6406 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6407 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6408 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6409 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6410 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6411 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6412 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6413 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6414 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6415 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6416 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6417 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6418 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6419 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6420 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6421 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6422 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6423 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6424 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6425 #ifdef MEMCACHED_ENABLE_DEPRECATED
6426 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6428 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6429 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6430 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6431 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6432 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6433 {"string", 0, 0, string_tests
},
6434 {"result", 0, 0, result_tests
},
6435 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6436 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6437 {"user", 0, 0, user_tests
},
6438 {"generate", 0, 0, generate_tests
},
6439 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6440 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6441 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6442 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6443 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6444 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6445 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6447 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6448 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6449 {"consistent_not", 0, 0, consistent_tests
},
6450 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6451 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6452 {"ketama_compat", 0, 0, ketama_compatibility
},
6453 {"test_hashes", 0, 0, hash_tests
},
6454 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6455 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6456 {"regression", 0, 0, regression_tests
},
6457 {"behaviors", 0, 0, behavior_tests
},
6458 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6459 {"error_conditions", 0, 0, error_conditions
},
6463 #define SERVERS_TO_CREATE 5
6465 #include "libmemcached_world.h"
6467 void get_world(world_st
*world
)
6469 world
->collections
= collection
;
6471 world
->create
= (test_callback_create_fn
)world_create
;
6472 world
->destroy
= (test_callback_fn
)world_destroy
;
6474 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6475 world
->test
.flush
= (test_callback_fn
)world_flush
;
6476 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6477 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6478 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6480 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6481 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6483 world
->runner
= &defualt_libmemcached_runner
;