1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 Sample test application.
56 #include <sys/types.h>
59 #include "libmemcached/common.h"
61 #include <libtest/server.h>
63 #include "clients/generator.h"
64 #include "clients/execute.h"
66 #define SMALL_STRING_LEN 1024
68 #include <libtest/test.h>
69 #include "tests/deprecated.h"
70 #include "tests/parser.h"
71 #include "tests/pool.h"
72 #include "tests/string.h"
73 #include "tests/replication.h"
74 #include "tests/basic.h"
75 #include "tests/error_conditions.h"
76 #include "tests/print.h"
77 #include "tests/virtual_buckets.h"
80 #ifdef HAVE_LIBMEMCACHEDUTIL
82 #include "libmemcached/memcached_util.h"
85 #include "hash_results.h"
87 #define GLOBAL_COUNT 10000
88 #define GLOBAL2_COUNT 100
89 #define SERVERS_TO_CREATE 5
90 static uint32_t global_count
;
92 static pairs_st
*global_pairs
;
93 static const char *global_keys
[GLOBAL_COUNT
];
94 static size_t global_keys_length
[GLOBAL_COUNT
];
97 static test_return_t
pre_binary(memcached_st
*memc
);
100 static test_return_t
init_test(memcached_st
*not_used
)
105 (void)memcached_create(&memc
);
106 memcached_free(&memc
);
111 #define TEST_PORT_COUNT 7
112 in_port_t test_ports
[TEST_PORT_COUNT
];
114 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
115 const memcached_server_st
*server
,
119 size_t bigger
= *((size_t *)(context
));
121 assert(bigger
<= memcached_server_port(server
));
122 *((size_t *)(context
))= memcached_server_port(server
);
124 return MEMCACHED_SUCCESS
;
127 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
128 const memcached_server_st
*instance
,
132 FILE *stream
= (FILE *)context
;
135 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
136 memcached_server_name(instance
),
137 memcached_server_port(instance
),
138 instance
->major_version
,
139 instance
->minor_version
,
140 instance
->micro_version
);
142 return MEMCACHED_SUCCESS
;
145 static test_return_t
server_sort_test(memcached_st
*ptr
)
147 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
149 memcached_return_t rc
;
150 memcached_server_fn callbacks
[1];
151 memcached_st
*local_memc
;
154 local_memc
= memcached_create(NULL
);
155 test_true(local_memc
);
156 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
158 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
160 test_ports
[x
]= (in_port_t
)random() % 64000;
161 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
162 test_true(memcached_server_count(local_memc
) == x
+ 1);
164 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
166 test_true(rc
== MEMCACHED_SUCCESS
);
169 callbacks
[0]= server_display_function
;
170 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
173 memcached_free(local_memc
);
178 static test_return_t
server_sort2_test(memcached_st
*ptr
)
180 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
181 memcached_return_t rc
;
182 memcached_server_fn callbacks
[1];
183 memcached_st
*local_memc
;
184 memcached_server_instance_st instance
;
187 local_memc
= memcached_create(NULL
);
188 test_true(local_memc
);
189 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
190 test_true(rc
== MEMCACHED_SUCCESS
);
192 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
193 test_true(rc
== MEMCACHED_SUCCESS
);
194 instance
= memcached_server_instance_by_position(local_memc
, 0);
195 test_true(memcached_server_port(instance
) == 43043);
197 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
198 test_true(rc
== MEMCACHED_SUCCESS
);
200 instance
= memcached_server_instance_by_position(local_memc
, 0);
201 test_true(memcached_server_port(instance
) == 43042);
203 instance
= memcached_server_instance_by_position(local_memc
, 1);
204 test_true(memcached_server_port(instance
) == 43043);
206 callbacks
[0]= server_display_function
;
207 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
210 memcached_free(local_memc
);
215 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
217 const char *server_string
= "--server=localhost:4444 --server=localhost:4445 --server=localhost:4446 --server=localhost:4447 --server=localhost --server=memcache1.memcache.bk.sapo.pt:11211 --server=memcache1.memcache.bk.sapo.pt:11212 --server=memcache1.memcache.bk.sapo.pt:11213 --server=memcache1.memcache.bk.sapo.pt:11214 --server=memcache2.memcache.bk.sapo.pt:11211 --server=memcache2.memcache.bk.sapo.pt:11212 --server=memcache2.memcache.bk.sapo.pt:11213 --server=memcache2.memcache.bk.sapo.pt:11214";
220 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
223 memcached_server_fn callbacks
[1];
224 callbacks
[0]= server_print_callback
;
225 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
227 memcached_free(memc
);
232 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
233 const memcached_server_st
*server
,
237 uint32_t x
= *((uint32_t *)(context
));
240 if (! (test_ports
[x
] == server
->port
))
242 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)server
->port
);
243 return MEMCACHED_FAILURE
;
246 *((uint32_t *)(context
))= ++x
;
248 return MEMCACHED_SUCCESS
;
251 static test_return_t
server_unsort_test(memcached_st
*ptr
)
253 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
254 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
255 memcached_return_t rc
;
256 memcached_server_fn callbacks
[1];
257 memcached_st
*local_memc
;
260 local_memc
= memcached_create(NULL
);
261 test_true(local_memc
);
263 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
265 test_ports
[x
]= (in_port_t
)(random() % 64000);
266 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
267 test_true(memcached_server_count(local_memc
) == x
+1);
269 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
271 test_true(rc
== MEMCACHED_SUCCESS
);
274 callbacks
[0]= server_display_unsort_function
;
275 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
277 /* Now we sort old data! */
278 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
279 callbacks
[0]= server_display_function
;
280 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
283 memcached_free(local_memc
);
288 static test_return_t
allocation_test(memcached_st
*not_used
)
292 memc
= memcached_create(NULL
);
294 memcached_free(memc
);
299 static test_return_t
clone_test(memcached_st
*memc
)
303 memcached_st
*memc_clone
;
304 memc_clone
= memcached_clone(NULL
, NULL
);
305 test_true(memc_clone
);
306 memcached_free(memc_clone
);
309 /* Can we init from null? */
311 memcached_st
*memc_clone
;
312 memc_clone
= memcached_clone(NULL
, memc
);
313 test_true(memc_clone
);
316 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
317 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
318 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
319 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
322 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
323 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
324 test_true(memc_clone
->distribution
== memc
->distribution
);
325 { // Test all of the flags
326 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
327 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
328 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
329 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
330 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
331 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
332 test_true(memc_clone
->ketama
.weighted
== memc
->ketama
.weighted
);
333 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
334 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
335 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
336 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
337 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
338 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
340 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
341 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
342 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
343 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
344 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
345 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
346 test_true(memc_clone
->on_clone
== memc
->on_clone
);
347 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
348 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
349 test_true(memc_clone
->recv_size
== memc
->recv_size
);
350 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
351 test_true(memc_clone
->send_size
== memc
->send_size
);
352 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
353 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
354 test_true(memc_clone
->user_data
== memc
->user_data
);
356 memcached_free(memc_clone
);
359 /* Can we init from struct? */
361 memcached_st declared_clone
;
362 memcached_st
*memc_clone
;
363 memset(&declared_clone
, 0 , sizeof(memcached_st
));
364 memc_clone
= memcached_clone(&declared_clone
, NULL
);
365 test_true(memc_clone
);
366 memcached_free(memc_clone
);
369 /* Can we init from struct? */
371 memcached_st declared_clone
;
372 memcached_st
*memc_clone
;
373 memset(&declared_clone
, 0 , sizeof(memcached_st
));
374 memc_clone
= memcached_clone(&declared_clone
, memc
);
375 test_true(memc_clone
);
376 memcached_free(memc_clone
);
382 static test_return_t
userdata_test(memcached_st
*memc
)
385 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
386 test_true(memcached_get_user_data(memc
) == foo
);
387 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
392 static test_return_t
connection_test(memcached_st
*memc
)
394 memcached_return_t rc
;
396 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
397 test_true(rc
== MEMCACHED_SUCCESS
);
402 static test_return_t
error_test(memcached_st
*memc
)
404 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
405 982370485U, 1263635348U, 4242906218U, 3829656100U,
406 1891735253U, 334139633U, 2257084983U, 3088286104U,
407 13199785U, 2542027183U, 1097051614U, 199566778U,
408 2748246961U, 2465192557U, 1664094137U, 2405439045U,
409 1842224848U, 692413798U, 3479807801U, 919913813U,
410 4269430871U, 610793021U, 527273862U, 1437122909U,
411 2300930706U, 2943759320U, 674306647U, 2400528935U,
412 54481931U, 4186304426U, 1741088401U, 2979625118U,
413 4159057246U, 3425930182U, 2593724503U, 1868899624U,
414 1769812374U, 2302537950U, 1110330676U, 3365377466U,
415 1336171666U, 3021258493U, 3365377466U };
417 // You have updated the memcache_error messages but not updated docs/tests.
418 for (int rc
= int(MEMCACHED_SUCCESS
); rc
< int(MEMCACHED_MAXIMUM_RETURN
); ++rc
)
421 const char *msg
= memcached_strerror(memc
, memcached_return_t(rc
));
422 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
423 MEMCACHED_HASH_JENKINS
);
424 if (values
[rc
] != hash_val
)
426 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
427 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, memcached_return_t(rc
)), hash_val
);
429 test_true(values
[rc
] == hash_val
);
431 test_true(MEMCACHED_MAXIMUM_RETURN
== 46);
436 static test_return_t
set_test(memcached_st
*memc
)
438 memcached_return_t rc
;
439 const char *key
= "foo";
440 const char *value
= "when we sanitize";
442 rc
= memcached_set(memc
, key
, strlen(key
),
443 value
, strlen(value
),
444 (time_t)0, (uint32_t)0);
445 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
450 static test_return_t
append_test(memcached_st
*memc
)
452 memcached_return_t rc
;
453 const char *key
= "fig";
454 const char *in_value
= "we";
455 char *out_value
= NULL
;
459 rc
= memcached_flush(memc
, 0);
460 test_true(rc
== MEMCACHED_SUCCESS
);
462 rc
= memcached_set(memc
, key
, strlen(key
),
463 in_value
, strlen(in_value
),
464 (time_t)0, (uint32_t)0);
465 test_true(rc
== MEMCACHED_SUCCESS
);
467 rc
= memcached_append(memc
, key
, strlen(key
),
468 " the", strlen(" the"),
469 (time_t)0, (uint32_t)0);
470 test_true(rc
== MEMCACHED_SUCCESS
);
472 rc
= memcached_append(memc
, key
, strlen(key
),
473 " people", strlen(" people"),
474 (time_t)0, (uint32_t)0);
475 test_true(rc
== MEMCACHED_SUCCESS
);
477 out_value
= memcached_get(memc
, key
, strlen(key
),
478 &value_length
, &flags
, &rc
);
479 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
480 test_true(strlen("we the people") == value_length
);
481 test_true(rc
== MEMCACHED_SUCCESS
);
487 static test_return_t
append_binary_test(memcached_st
*memc
)
489 memcached_return_t rc
;
490 const char *key
= "numbers";
491 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
497 rc
= memcached_flush(memc
, 0);
498 test_true(rc
== MEMCACHED_SUCCESS
);
500 rc
= memcached_set(memc
,
503 (time_t)0, (uint32_t)0);
504 test_true(rc
== MEMCACHED_SUCCESS
);
506 for (x
= 0; store_list
[x
] ; x
++)
508 rc
= memcached_append(memc
,
510 (char *)&store_list
[x
], sizeof(uint32_t),
511 (time_t)0, (uint32_t)0);
512 test_true(rc
== MEMCACHED_SUCCESS
);
515 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
516 &value_length
, &flags
, &rc
);
517 test_true((value_length
== (sizeof(uint32_t) * x
)));
518 test_true(rc
== MEMCACHED_SUCCESS
);
520 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
522 test_true(*ptr
== store_list
[x
- counter
]);
530 static test_return_t
cas2_test(memcached_st
*memc
)
532 memcached_return_t rc
;
533 const char *keys
[]= {"fudge", "son", "food"};
534 size_t key_length
[]= {5, 3, 4};
535 const char *value
= "we the people";
536 size_t value_length
= strlen("we the people");
538 memcached_result_st results_obj
;
539 memcached_result_st
*results
;
542 rc
= memcached_flush(memc
, 0);
543 test_true(rc
== MEMCACHED_SUCCESS
);
545 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
547 for (x
= 0; x
< 3; x
++)
549 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
550 keys
[x
], key_length
[x
],
551 (time_t)50, (uint32_t)9);
552 test_true(rc
== MEMCACHED_SUCCESS
);
555 rc
= memcached_mget(memc
, keys
, key_length
, 3);
557 results
= memcached_result_create(memc
, &results_obj
);
559 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
561 test_true(results
->item_cas
);
562 test_true(rc
== MEMCACHED_SUCCESS
);
563 test_true(memcached_result_cas(results
));
565 test_true(!memcmp(value
, "we the people", strlen("we the people")));
566 test_true(strlen("we the people") == value_length
);
567 test_true(rc
== MEMCACHED_SUCCESS
);
569 memcached_result_free(&results_obj
);
574 static test_return_t
cas_test(memcached_st
*memc
)
576 memcached_return_t rc
;
577 const char *key
= "fun";
578 size_t key_length
= strlen(key
);
579 const char *value
= "we the people";
580 const char* keys
[2] = { key
, NULL
};
581 size_t keylengths
[2] = { strlen(key
), 0 };
582 size_t value_length
= strlen(value
);
583 const char *value2
= "change the value";
584 size_t value2_length
= strlen(value2
);
586 memcached_result_st results_obj
;
587 memcached_result_st
*results
;
590 rc
= memcached_flush(memc
, 0);
591 test_true(rc
== MEMCACHED_SUCCESS
);
593 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
595 rc
= memcached_set(memc
, key
, strlen(key
),
596 value
, strlen(value
),
597 (time_t)0, (uint32_t)0);
598 test_true(rc
== MEMCACHED_SUCCESS
);
600 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
602 results
= memcached_result_create(memc
, &results_obj
);
604 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
606 test_true(rc
== MEMCACHED_SUCCESS
);
607 test_true(memcached_result_cas(results
));
608 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
609 test_true(strlen(memcached_result_value(results
)) == value_length
);
610 test_true(rc
== MEMCACHED_SUCCESS
);
611 uint64_t cas
= memcached_result_cas(results
);
614 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
615 test_true(rc
== MEMCACHED_END
);
616 test_true(results
== NULL
);
619 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
620 test_true(rc
== MEMCACHED_SUCCESS
);
623 * The item will have a new cas value, so try to set it again with the old
624 * value. This should fail!
626 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
627 test_true(rc
== MEMCACHED_DATA_EXISTS
);
629 memcached_result_free(&results_obj
);
634 static test_return_t
prepend_test(memcached_st
*memc
)
636 memcached_return_t rc
;
637 const char *key
= "fig";
638 const char *value
= "people";
639 char *out_value
= NULL
;
643 rc
= memcached_flush(memc
, 0);
644 test_true(rc
== MEMCACHED_SUCCESS
);
646 rc
= memcached_set(memc
, key
, strlen(key
),
647 value
, strlen(value
),
648 (time_t)0, (uint32_t)0);
649 test_true(rc
== MEMCACHED_SUCCESS
);
651 rc
= memcached_prepend(memc
, key
, strlen(key
),
652 "the ", strlen("the "),
653 (time_t)0, (uint32_t)0);
654 test_true(rc
== MEMCACHED_SUCCESS
);
656 rc
= memcached_prepend(memc
, key
, strlen(key
),
657 "we ", strlen("we "),
658 (time_t)0, (uint32_t)0);
659 test_true(rc
== MEMCACHED_SUCCESS
);
661 out_value
= memcached_get(memc
, key
, strlen(key
),
662 &value_length
, &flags
, &rc
);
663 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
664 test_true(strlen("we the people") == value_length
);
665 test_true(rc
== MEMCACHED_SUCCESS
);
672 Set the value, then quit to make sure it is flushed.
673 Come back in and test that add fails.
675 static test_return_t
add_test(memcached_st
*memc
)
677 memcached_return_t rc
;
678 const char *key
= "foo";
679 const char *value
= "when we sanitize";
680 unsigned long long setting_value
;
682 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
684 rc
= memcached_set(memc
, key
, strlen(key
),
685 value
, strlen(value
),
686 (time_t)0, (uint32_t)0);
687 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
688 memcached_quit(memc
);
689 rc
= memcached_add(memc
, key
, strlen(key
),
690 value
, strlen(value
),
691 (time_t)0, (uint32_t)0);
693 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
696 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
700 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
707 ** There was a problem of leaking filedescriptors in the initial release
708 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
709 ** systems it seems that the kernel is slow on reclaiming the resources
710 ** because the connects starts to time out (the test doesn't do much
711 ** anyway, so just loop 10 iterations)
713 static test_return_t
add_wrapper(memcached_st
*memc
)
715 unsigned int max
= 10000;
723 for (uint32_t x
= 0; x
< max
; x
++)
729 static test_return_t
replace_test(memcached_st
*memc
)
731 memcached_return_t rc
;
732 const char *key
= "foo";
733 const char *value
= "when we sanitize";
734 const char *original
= "first we insert some data";
736 rc
= memcached_set(memc
, key
, strlen(key
),
737 original
, strlen(original
),
738 (time_t)0, (uint32_t)0);
739 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
741 rc
= memcached_replace(memc
, key
, strlen(key
),
742 value
, strlen(value
),
743 (time_t)0, (uint32_t)0);
744 test_true(rc
== MEMCACHED_SUCCESS
);
749 static test_return_t
delete_test(memcached_st
*memc
)
751 memcached_return_t rc
;
752 const char *key
= "foo";
753 const char *value
= "when we sanitize";
755 rc
= memcached_set(memc
, key
, strlen(key
),
756 value
, strlen(value
),
757 (time_t)0, (uint32_t)0);
758 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
760 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
761 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
766 static test_return_t
flush_test(memcached_st
*memc
)
768 memcached_return_t rc
;
770 uint64_t query_id
= memcached_query_id(memc
);
771 rc
= memcached_flush(memc
, 0);
772 test_compare(rc
, MEMCACHED_SUCCESS
);
773 test_compare(query_id
+1, memcached_query_id(memc
));
778 static memcached_return_t
server_function(const memcached_st
*ptr
,
779 const memcached_server_st
*server
,
782 (void)ptr
; (void)server
; (void)context
;
785 return MEMCACHED_SUCCESS
;
788 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
791 strncpy(context
, "foo bad", sizeof(context
));
792 memcached_server_fn callbacks
[1];
794 callbacks
[0]= server_function
;
795 memcached_server_cursor(memc
, callbacks
, context
, 1);
799 static test_return_t
bad_key_test(memcached_st
*memc
)
801 memcached_return_t rc
;
802 const char *key
= "foo bad";
804 size_t string_length
;
806 memcached_st
*memc_clone
;
808 size_t max_keylen
= 0xffff;
810 // Just skip if we are in binary mode.
811 uint64_t query_id
= memcached_query_id(memc
);
812 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
814 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
816 memc_clone
= memcached_clone(NULL
, memc
);
817 test_true(memc_clone
);
819 query_id
= memcached_query_id(memc_clone
);
820 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
821 test_true(rc
== MEMCACHED_SUCCESS
);
822 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
824 /* All keys are valid in the binary protocol (except for length) */
825 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
827 query_id
= memcached_query_id(memc_clone
);
828 string
= memcached_get(memc_clone
, key
, strlen(key
),
829 &string_length
, &flags
, &rc
);
830 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
831 test_true(string_length
== 0);
835 query_id
= memcached_query_id(memc_clone
);
836 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
837 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
838 test_true(rc
== MEMCACHED_SUCCESS
);
839 string
= memcached_get(memc_clone
, key
, strlen(key
),
840 &string_length
, &flags
, &rc
);
841 test_true(rc
== MEMCACHED_NOTFOUND
);
842 test_true(string_length
== 0);
845 /* Test multi key for bad keys */
846 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
847 size_t key_lengths
[] = { 7, 7, 7 };
849 query_id
= memcached_query_id(memc_clone
);
850 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
851 test_true(rc
== MEMCACHED_SUCCESS
);
852 test_compare(query_id
, memcached_query_id(memc_clone
));
854 query_id
= memcached_query_id(memc_clone
);
855 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
856 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
857 test_compare(query_id
+1, memcached_query_id(memc_clone
));
859 query_id
= memcached_query_id(memc_clone
);
860 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
861 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
862 test_compare(query_id
+1, memcached_query_id(memc_clone
));
866 /* The following test should be moved to the end of this function when the
867 memcached server is updated to allow max size length of the keys in the
870 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
871 test_true(rc
== MEMCACHED_SUCCESS
);
873 char *longkey
= (char *)malloc(max_keylen
+ 1);
876 memset(longkey
, 'a', max_keylen
+ 1);
877 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
878 &string_length
, &flags
, &rc
);
879 test_true(rc
== MEMCACHED_NOTFOUND
);
880 test_true(string_length
== 0);
883 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
884 &string_length
, &flags
, &rc
);
885 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
886 test_true(string_length
== 0);
893 /* Make sure zero length keys are marked as bad */
895 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
896 test_true(rc
== MEMCACHED_SUCCESS
);
897 string
= memcached_get(memc_clone
, key
, 0,
898 &string_length
, &flags
, &rc
);
899 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
900 test_true(string_length
== 0);
903 memcached_free(memc_clone
);
908 #define READ_THROUGH_VALUE "set for me"
909 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
912 memcached_result_st
*result
)
914 (void)memc
;(void)key
;(void)key_length
;
915 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
918 #ifndef __INTEL_COMPILER
919 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
922 static test_return_t
read_through(memcached_st
*memc
)
924 memcached_return_t rc
;
925 const char *key
= "foo";
927 size_t string_length
;
929 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
931 string
= memcached_get(memc
, key
, strlen(key
),
932 &string_length
, &flags
, &rc
);
934 test_true(rc
== MEMCACHED_NOTFOUND
);
935 test_false(string_length
);
938 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, *(void **)&cb
);
939 test_true(rc
== MEMCACHED_SUCCESS
);
941 string
= memcached_get(memc
, key
, strlen(key
),
942 &string_length
, &flags
, &rc
);
944 test_true(rc
== MEMCACHED_SUCCESS
);
945 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
946 test_strcmp(READ_THROUGH_VALUE
, string
);
949 string
= memcached_get(memc
, key
, strlen(key
),
950 &string_length
, &flags
, &rc
);
952 test_true(rc
== MEMCACHED_SUCCESS
);
953 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
954 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
960 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
964 (void)ptr
;(void)key_length
;
967 return MEMCACHED_SUCCESS
;
970 static test_return_t
delete_through(memcached_st
*memc
)
972 memcached_trigger_delete_key_fn callback
;
973 memcached_return_t rc
;
975 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
977 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
978 test_true(rc
== MEMCACHED_SUCCESS
);
983 static test_return_t
get_test(memcached_st
*memc
)
985 memcached_return_t rc
;
986 const char *key
= "foo";
988 size_t string_length
;
991 uint64_t query_id
= memcached_query_id(memc
);
992 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
993 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
994 test_compare(query_id
+1, memcached_query_id(memc
));
996 string
= memcached_get(memc
, key
, strlen(key
),
997 &string_length
, &flags
, &rc
);
999 test_true(rc
== MEMCACHED_NOTFOUND
);
1000 test_false(string_length
);
1003 return TEST_SUCCESS
;
1006 static test_return_t
get_test2(memcached_st
*memc
)
1008 memcached_return_t rc
;
1009 const char *key
= "foo";
1010 const char *value
= "when we sanitize";
1012 size_t string_length
;
1015 uint64_t query_id
= memcached_query_id(memc
);
1016 rc
= memcached_set(memc
, key
, strlen(key
),
1017 value
, strlen(value
),
1018 (time_t)0, (uint32_t)0);
1019 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1020 test_compare(query_id
+1, memcached_query_id(memc
));
1022 query_id
= memcached_query_id(memc
);
1023 string
= memcached_get(memc
, key
, strlen(key
),
1024 &string_length
, &flags
, &rc
);
1025 test_compare(query_id
+1, memcached_query_id(memc
));
1028 test_true(rc
== MEMCACHED_SUCCESS
);
1029 test_true(string_length
== strlen(value
));
1030 test_memcmp(string
, value
, string_length
);
1034 return TEST_SUCCESS
;
1037 static test_return_t
set_test2(memcached_st
*memc
)
1039 memcached_return_t rc
;
1040 const char *key
= "foo";
1041 const char *value
= "train in the brain";
1042 size_t value_length
= strlen(value
);
1045 for (x
= 0; x
< 10; x
++)
1047 rc
= memcached_set(memc
, key
, strlen(key
),
1048 value
, value_length
,
1049 (time_t)0, (uint32_t)0);
1050 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1053 return TEST_SUCCESS
;
1056 static test_return_t
set_test3(memcached_st
*memc
)
1058 memcached_return_t rc
;
1060 size_t value_length
= 8191;
1062 value
= (char*)malloc(value_length
);
1065 for (uint32_t x
= 0; x
< value_length
; x
++)
1066 value
[x
] = (char) (x
% 127);
1068 /* The dump test relies on there being at least 32 items in memcached */
1069 for (uint32_t x
= 0; x
< 32; x
++)
1073 snprintf(key
, sizeof(key
), "foo%u", x
);
1075 uint64_t query_id
= memcached_query_id(memc
);
1076 rc
= memcached_set(memc
, key
, strlen(key
),
1077 value
, value_length
,
1078 (time_t)0, (uint32_t)0);
1079 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1080 test_compare(query_id
+1, memcached_query_id(memc
));
1085 return TEST_SUCCESS
;
1088 static test_return_t
get_test3(memcached_st
*memc
)
1090 memcached_return_t rc
;
1091 const char *key
= "foo";
1093 size_t value_length
= 8191;
1095 size_t string_length
;
1099 value
= (char*)malloc(value_length
);
1102 for (x
= 0; x
< value_length
; x
++)
1103 value
[x
] = (char) (x
% 127);
1105 rc
= memcached_set(memc
, key
, strlen(key
),
1106 value
, value_length
,
1107 (time_t)0, (uint32_t)0);
1108 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1110 string
= memcached_get(memc
, key
, strlen(key
),
1111 &string_length
, &flags
, &rc
);
1113 test_true(rc
== MEMCACHED_SUCCESS
);
1115 test_true(string_length
== value_length
);
1116 test_true(!memcmp(string
, value
, string_length
));
1121 return TEST_SUCCESS
;
1124 static test_return_t
get_test4(memcached_st
*memc
)
1126 memcached_return_t rc
;
1127 const char *key
= "foo";
1129 size_t value_length
= 8191;
1131 size_t string_length
;
1135 value
= (char*)malloc(value_length
);
1138 for (x
= 0; x
< value_length
; x
++)
1139 value
[x
] = (char) (x
% 127);
1141 rc
= memcached_set(memc
, key
, strlen(key
),
1142 value
, value_length
,
1143 (time_t)0, (uint32_t)0);
1144 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1146 for (x
= 0; x
< 10; x
++)
1148 string
= memcached_get(memc
, key
, strlen(key
),
1149 &string_length
, &flags
, &rc
);
1151 test_true(rc
== MEMCACHED_SUCCESS
);
1153 test_true(string_length
== value_length
);
1154 test_true(!memcmp(string
, value
, string_length
));
1160 return TEST_SUCCESS
;
1164 * This test verifies that memcached_read_one_response doesn't try to
1165 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1166 * responses before you execute a storage command.
1168 static test_return_t
get_test5(memcached_st
*memc
)
1171 ** Request the same key twice, to ensure that we hash to the same server
1172 ** (so that we have multiple response values queued up) ;-)
1174 const char *keys
[]= { "key", "key" };
1175 size_t lengths
[]= { 3, 3 };
1179 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1180 keys
[0], lengths
[0], 0, 0);
1181 test_true(rc
== MEMCACHED_SUCCESS
);
1182 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1184 memcached_result_st results_obj
;
1185 memcached_result_st
*results
;
1186 results
=memcached_result_create(memc
, &results_obj
);
1188 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1190 memcached_result_free(&results_obj
);
1192 /* Don't read out the second result, but issue a set instead.. */
1193 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1194 test_true(rc
== MEMCACHED_SUCCESS
);
1196 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1197 &rlen
, &flags
, &rc
);
1198 test_true(val
== NULL
);
1199 test_true(rc
== MEMCACHED_NOTFOUND
);
1200 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1201 test_true(val
!= NULL
);
1202 test_true(rc
== MEMCACHED_SUCCESS
);
1205 return TEST_SUCCESS
;
1208 static test_return_t
mget_end(memcached_st
*memc
)
1210 const char *keys
[]= { "foo", "foo2" };
1211 size_t lengths
[]= { 3, 4 };
1212 const char *values
[]= { "fjord", "41" };
1214 memcached_return_t rc
;
1217 for (int i
= 0; i
< 2; i
++)
1219 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1220 (time_t)0, (uint32_t)0);
1221 test_true(rc
== MEMCACHED_SUCCESS
);
1225 size_t string_length
;
1228 // retrieve both via mget
1229 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1230 test_true(rc
== MEMCACHED_SUCCESS
);
1232 char key
[MEMCACHED_MAX_KEY
];
1235 // this should get both
1236 for (int i
= 0; i
< 2; i
++)
1238 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1240 test_true(rc
== MEMCACHED_SUCCESS
);
1242 if (key_length
== 4)
1244 test_true(string_length
== strlen(values
[val
]));
1245 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1249 // this should indicate end
1250 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1251 test_true(rc
== MEMCACHED_END
);
1254 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1255 test_true(rc
== MEMCACHED_SUCCESS
);
1257 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1258 test_true(key_length
== lengths
[0]);
1259 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1260 test_true(string_length
== strlen(values
[0]));
1261 test_true(strncmp(values
[0], string
, string_length
) == 0);
1262 test_true(rc
== MEMCACHED_SUCCESS
);
1265 // this should indicate end
1266 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1267 test_true(rc
== MEMCACHED_END
);
1269 return TEST_SUCCESS
;
1272 /* Do not copy the style of this code, I just access hosts to testthis function */
1273 static test_return_t
stats_servername_test(memcached_st
*memc
)
1275 memcached_return_t rc
;
1276 memcached_stat_st memc_stat
;
1277 memcached_server_instance_st instance
=
1278 memcached_server_instance_by_position(memc
, 0);
1280 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1281 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1282 return TEST_SKIPPED
;
1284 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1285 memcached_server_name(instance
),
1286 memcached_server_port(instance
));
1288 return TEST_SUCCESS
;
1291 static test_return_t
increment_test(memcached_st
*memc
)
1293 uint64_t new_number
;
1294 memcached_return_t rc
;
1295 const char *key
= "number";
1296 const char *value
= "0";
1298 rc
= memcached_set(memc
, key
, strlen(key
),
1299 value
, strlen(value
),
1300 (time_t)0, (uint32_t)0);
1301 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1303 rc
= memcached_increment(memc
, key
, strlen(key
),
1305 test_true(rc
== MEMCACHED_SUCCESS
);
1306 test_true(new_number
== 1);
1308 rc
= memcached_increment(memc
, key
, strlen(key
),
1310 test_true(rc
== MEMCACHED_SUCCESS
);
1311 test_true(new_number
== 2);
1313 return TEST_SUCCESS
;
1316 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1318 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1320 uint64_t new_number
;
1321 memcached_return_t rc
;
1322 const char *key
= "number";
1323 uint64_t initial
= 0;
1325 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1326 1, initial
, 0, &new_number
);
1327 test_true(rc
== MEMCACHED_SUCCESS
);
1328 test_true(new_number
== initial
);
1330 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1331 1, initial
, 0, &new_number
);
1332 test_true(rc
== MEMCACHED_SUCCESS
);
1333 test_true(new_number
== (initial
+ 1));
1335 return TEST_SUCCESS
;
1338 static test_return_t
decrement_test(memcached_st
*memc
)
1340 uint64_t new_number
;
1341 memcached_return_t rc
;
1342 const char *key
= "number";
1343 const char *value
= "3";
1345 rc
= memcached_set(memc
, key
, strlen(key
),
1346 value
, strlen(value
),
1347 (time_t)0, (uint32_t)0);
1348 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1350 rc
= memcached_decrement(memc
, key
, strlen(key
),
1352 test_true(rc
== MEMCACHED_SUCCESS
);
1353 test_true(new_number
== 2);
1355 rc
= memcached_decrement(memc
, key
, strlen(key
),
1357 test_true(rc
== MEMCACHED_SUCCESS
);
1358 test_true(new_number
== 1);
1360 return TEST_SUCCESS
;
1363 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1365 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1367 uint64_t new_number
;
1368 memcached_return_t rc
;
1369 const char *key
= "number";
1370 uint64_t initial
= 3;
1372 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1373 1, initial
, 0, &new_number
);
1374 test_true(rc
== MEMCACHED_SUCCESS
);
1375 test_true(new_number
== initial
);
1377 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1378 1, initial
, 0, &new_number
);
1379 test_true(rc
== MEMCACHED_SUCCESS
);
1380 test_true(new_number
== (initial
- 1));
1382 return TEST_SUCCESS
;
1385 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1387 uint64_t new_number
;
1388 memcached_return_t rc
;
1389 const char *master_key
= "foo";
1390 const char *key
= "number";
1391 const char *value
= "0";
1393 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1395 value
, strlen(value
),
1396 (time_t)0, (uint32_t)0);
1397 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1399 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1401 test_true(rc
== MEMCACHED_SUCCESS
);
1402 test_true(new_number
== 1);
1404 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1406 test_true(rc
== MEMCACHED_SUCCESS
);
1407 test_true(new_number
== 2);
1409 return TEST_SUCCESS
;
1412 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1414 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1416 uint64_t new_number
;
1417 memcached_return_t rc
;
1418 const char *master_key
= "foo";
1419 const char *key
= "number";
1420 uint64_t initial
= 0;
1422 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1424 1, initial
, 0, &new_number
);
1425 test_true(rc
== MEMCACHED_SUCCESS
);
1426 test_true(new_number
== initial
);
1428 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1430 1, initial
, 0, &new_number
);
1431 test_true(rc
== MEMCACHED_SUCCESS
);
1432 test_true(new_number
== (initial
+ 1));
1434 return TEST_SUCCESS
;
1437 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1439 uint64_t new_number
;
1440 memcached_return_t rc
;
1441 const char *master_key
= "foo";
1442 const char *key
= "number";
1443 const char *value
= "3";
1445 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1447 value
, strlen(value
),
1448 (time_t)0, (uint32_t)0);
1449 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1451 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1454 test_true(rc
== MEMCACHED_SUCCESS
);
1455 test_true(new_number
== 2);
1457 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1460 test_true(rc
== MEMCACHED_SUCCESS
);
1461 test_true(new_number
== 1);
1463 return TEST_SUCCESS
;
1466 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1468 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1470 uint64_t new_number
;
1471 memcached_return_t rc
;
1472 const char *master_key
= "foo";
1473 const char *key
= "number";
1474 uint64_t initial
= 3;
1476 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1478 1, initial
, 0, &new_number
);
1479 test_true(rc
== MEMCACHED_SUCCESS
);
1480 test_true(new_number
== initial
);
1482 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1484 1, initial
, 0, &new_number
);
1485 test_true(rc
== MEMCACHED_SUCCESS
);
1486 test_true(new_number
== (initial
- 1));
1488 return TEST_SUCCESS
;
1491 static test_return_t
quit_test(memcached_st
*memc
)
1493 memcached_return_t rc
;
1494 const char *key
= "fudge";
1495 const char *value
= "sanford and sun";
1497 rc
= memcached_set(memc
, key
, strlen(key
),
1498 value
, strlen(value
),
1499 (time_t)10, (uint32_t)3);
1500 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1501 memcached_quit(memc
);
1503 rc
= memcached_set(memc
, key
, strlen(key
),
1504 value
, strlen(value
),
1505 (time_t)50, (uint32_t)9);
1506 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1508 return TEST_SUCCESS
;
1511 static test_return_t
mget_result_test(memcached_st
*memc
)
1513 memcached_return_t rc
;
1514 const char *keys
[]= {"fudge", "son", "food"};
1515 size_t key_length
[]= {5, 3, 4};
1518 memcached_result_st results_obj
;
1519 memcached_result_st
*results
;
1521 results
= memcached_result_create(memc
, &results_obj
);
1523 test_true(&results_obj
== results
);
1525 /* We need to empty the server before continueing test */
1526 rc
= memcached_flush(memc
, 0);
1527 test_true(rc
== MEMCACHED_SUCCESS
);
1529 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1530 test_true(rc
== MEMCACHED_SUCCESS
);
1532 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1537 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1538 test_true(!results
);
1539 test_true(rc
== MEMCACHED_END
);
1541 for (x
= 0; x
< 3; x
++)
1543 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1544 keys
[x
], key_length
[x
],
1545 (time_t)50, (uint32_t)9);
1546 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1549 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1550 test_true(rc
== MEMCACHED_SUCCESS
);
1552 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1555 test_true(&results_obj
== results
);
1556 test_true(rc
== MEMCACHED_SUCCESS
);
1557 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1558 test_true(!memcmp(memcached_result_key_value(results
),
1559 memcached_result_value(results
),
1560 memcached_result_length(results
)));
1563 memcached_result_free(&results_obj
);
1565 return TEST_SUCCESS
;
1568 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1570 memcached_return_t rc
;
1571 const char *keys
[]= {"fudge", "son", "food"};
1572 size_t key_length
[]= {5, 3, 4};
1575 memcached_result_st
*results
;
1577 /* We need to empty the server before continueing test */
1578 rc
= memcached_flush(memc
, 0);
1579 test_true(rc
== MEMCACHED_SUCCESS
);
1581 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1582 test_true(rc
== MEMCACHED_SUCCESS
);
1584 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1588 test_true(!results
);
1589 test_true(rc
== MEMCACHED_END
);
1591 for (x
= 0; x
< 3; x
++)
1593 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1594 keys
[x
], key_length
[x
],
1595 (time_t)50, (uint32_t)9);
1596 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1599 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1600 test_true(rc
== MEMCACHED_SUCCESS
);
1603 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1606 test_true(rc
== MEMCACHED_SUCCESS
);
1607 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1608 test_true(!memcmp(memcached_result_key_value(results
),
1609 memcached_result_value(results
),
1610 memcached_result_length(results
)));
1611 memcached_result_free(results
);
1615 return TEST_SUCCESS
;
1618 /* Count the results */
1619 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1620 memcached_result_st
*result
,
1623 (void)ptr
; (void)result
;
1624 size_t *counter
= (size_t *)context
;
1626 *counter
= *counter
+ 1;
1628 return MEMCACHED_SUCCESS
;
1631 static test_return_t
mget_result_function(memcached_st
*memc
)
1633 memcached_return_t rc
;
1634 const char *keys
[]= {"fudge", "son", "food"};
1635 size_t key_length
[]= {5, 3, 4};
1638 memcached_execute_fn callbacks
[1];
1640 /* We need to empty the server before continueing test */
1641 rc
= memcached_flush(memc
, 0);
1642 for (x
= 0; x
< 3; x
++)
1644 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1645 keys
[x
], key_length
[x
],
1646 (time_t)50, (uint32_t)9);
1647 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1650 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1651 test_true(rc
== MEMCACHED_SUCCESS
);
1653 callbacks
[0]= &callback_counter
;
1655 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1657 test_true(counter
== 3);
1659 return TEST_SUCCESS
;
1662 static test_return_t
mget_test(memcached_st
*memc
)
1664 memcached_return_t rc
;
1665 const char *keys
[]= {"fudge", "son", "food"};
1666 size_t key_length
[]= {5, 3, 4};
1670 char return_key
[MEMCACHED_MAX_KEY
];
1671 size_t return_key_length
;
1673 size_t return_value_length
;
1675 /* We need to empty the server before continueing test */
1676 rc
= memcached_flush(memc
, 0);
1677 test_true(rc
== MEMCACHED_SUCCESS
);
1679 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1680 test_true(rc
== MEMCACHED_SUCCESS
);
1682 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1683 &return_value_length
, &flags
, &rc
)) != NULL
)
1685 test_true(return_value
);
1687 test_true(!return_value
);
1688 test_true(return_value_length
== 0);
1689 test_true(rc
== MEMCACHED_END
);
1691 for (x
= 0; x
< 3; x
++)
1693 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1694 keys
[x
], key_length
[x
],
1695 (time_t)50, (uint32_t)9);
1696 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1699 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1700 test_true(rc
== MEMCACHED_SUCCESS
);
1703 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1704 &return_value_length
, &flags
, &rc
)))
1706 test_true(return_value
);
1707 test_true(rc
== MEMCACHED_SUCCESS
);
1708 test_true(return_key_length
== return_value_length
);
1709 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1714 return TEST_SUCCESS
;
1717 static test_return_t
mget_execute(memcached_st
*memc
)
1721 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1725 * I only want to hit _one_ server so I know the number of requests I'm
1726 * sending in the pipeline.
1728 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1729 memc
->number_of_hosts
= 1;
1731 size_t max_keys
= 20480;
1734 char **keys
= static_cast<char **>(calloc(max_keys
, sizeof(char*)));
1735 size_t *key_length
=static_cast<size_t *>(calloc(max_keys
, sizeof(size_t)));
1737 /* First add all of the items.. */
1738 char blob
[1024] = {0};
1739 memcached_return_t rc
;
1741 for (size_t x
= 0; x
< max_keys
; ++x
)
1745 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1747 test_true(keys
[x
] != NULL
);
1748 uint64_t query_id
= memcached_query_id(memc
);
1749 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1750 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1751 test_compare(query_id
+1, memcached_query_id(memc
));
1754 /* Try to get all of them with a large multiget */
1756 memcached_execute_fn callbacks
[]= { &callback_counter
};
1757 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1758 max_keys
, callbacks
, &counter
, 1);
1760 if (rc
== MEMCACHED_SUCCESS
)
1763 uint64_t query_id
= memcached_query_id(memc
);
1764 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1765 test_true(rc
== MEMCACHED_END
);
1766 test_compare(query_id
, memcached_query_id(memc
));
1768 /* Verify that we got all of the items */
1769 test_true(counter
== max_keys
);
1771 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1773 test_true(counter
== 0);
1777 test_fail("note: this test functions differently when in binary mode");
1780 /* Release all allocated resources */
1781 for (size_t x
= 0; x
< max_keys
; ++x
)
1788 memc
->number_of_hosts
= number_of_hosts
;
1789 return TEST_SUCCESS
;
1792 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1794 static test_return_t
key_setup(memcached_st
*memc
)
1798 if (pre_binary(memc
) != TEST_SUCCESS
)
1799 return TEST_SKIPPED
;
1801 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1803 return TEST_SUCCESS
;
1806 static test_return_t
key_teardown(memcached_st
*memc
)
1809 pairs_free(global_pairs
);
1811 return TEST_SUCCESS
;
1814 static test_return_t
block_add_regression(memcached_st
*memc
)
1816 /* First add all of the items.. */
1817 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1819 memcached_return_t rc
;
1820 char blob
[1024] = {0};
1822 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1823 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1826 return TEST_SUCCESS
;
1829 static test_return_t
binary_add_regression(memcached_st
*memc
)
1831 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1832 test_return_t rc
= block_add_regression(memc
);
1833 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1837 static test_return_t
get_stats_keys(memcached_st
*memc
)
1841 memcached_stat_st memc_stat
;
1842 memcached_return_t rc
;
1844 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1845 test_true(rc
== MEMCACHED_SUCCESS
);
1846 for (ptr
= stat_list
; *ptr
; ptr
++)
1851 return TEST_SUCCESS
;
1854 static test_return_t
version_string_test(memcached_st
*memc
)
1856 const char *version_string
;
1859 version_string
= memcached_lib_version();
1861 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1863 return TEST_SUCCESS
;
1866 static test_return_t
get_stats(memcached_st
*memc
)
1870 memcached_return_t rc
;
1871 memcached_stat_st
*memc_stat
;
1873 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1874 test_true(rc
== MEMCACHED_SUCCESS
);
1876 test_true(rc
== MEMCACHED_SUCCESS
);
1877 test_true(memc_stat
);
1879 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1881 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1882 test_true(rc
== MEMCACHED_SUCCESS
);
1883 for (ptr
= stat_list
; *ptr
; ptr
++);
1888 memcached_stat_free(NULL
, memc_stat
);
1890 return TEST_SUCCESS
;
1893 static test_return_t
add_host_test(memcached_st
*memc
)
1896 memcached_server_st
*servers
;
1897 memcached_return_t rc
;
1898 char servername
[]= "0.example.com";
1900 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1902 test_true(1 == memcached_server_list_count(servers
));
1904 for (x
= 2; x
< 20; x
++)
1906 char buffer
[SMALL_STRING_LEN
];
1908 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1909 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1911 test_true(rc
== MEMCACHED_SUCCESS
);
1912 test_true(x
== memcached_server_list_count(servers
));
1915 rc
= memcached_server_push(memc
, servers
);
1916 test_true(rc
== MEMCACHED_SUCCESS
);
1917 rc
= memcached_server_push(memc
, servers
);
1918 test_true(rc
== MEMCACHED_SUCCESS
);
1920 memcached_server_list_free(servers
);
1922 return TEST_SUCCESS
;
1925 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1927 (void)parent
;(void)memc_clone
;
1928 return MEMCACHED_SUCCESS
;
1931 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1934 return MEMCACHED_SUCCESS
;
1937 static test_return_t
callback_test(memcached_st
*memc
)
1939 /* Test User Data */
1943 memcached_return_t rc
;
1945 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1946 test_true(rc
== MEMCACHED_SUCCESS
);
1947 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1948 test_true(*test_ptr
== x
);
1951 /* Test Clone Callback */
1953 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1954 void *clone_cb_ptr
= *(void **)&clone_cb
;
1955 void *temp_function
= NULL
;
1956 memcached_return_t rc
;
1958 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1960 test_true(rc
== MEMCACHED_SUCCESS
);
1961 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1962 test_true(temp_function
== clone_cb_ptr
);
1965 /* Test Cleanup Callback */
1967 memcached_cleanup_fn cleanup_cb
=
1968 (memcached_cleanup_fn
)cleanup_test_callback
;
1969 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1970 void *temp_function
= NULL
;
1971 memcached_return_t rc
;
1973 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1975 test_true(rc
== MEMCACHED_SUCCESS
);
1976 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1977 test_true(temp_function
== cleanup_cb_ptr
);
1980 return TEST_SUCCESS
;
1983 /* We don't test the behavior itself, we test the switches */
1984 static test_return_t
behavior_test(memcached_st
*memc
)
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1990 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1991 test_true(value
== 1);
1993 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1994 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1995 test_true(value
== 1);
1997 set
= MEMCACHED_HASH_MD5
;
1998 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1999 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2000 test_true(value
== MEMCACHED_HASH_MD5
);
2004 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2005 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
2006 test_true(value
== 0);
2008 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2009 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
2010 test_true(value
== 0);
2012 set
= MEMCACHED_HASH_DEFAULT
;
2013 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2014 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2015 test_true(value
== MEMCACHED_HASH_DEFAULT
);
2017 set
= MEMCACHED_HASH_CRC
;
2018 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2019 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2020 test_true(value
== MEMCACHED_HASH_CRC
);
2022 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2023 test_true(value
> 0);
2025 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2026 test_true(value
> 0);
2028 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2029 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
2030 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2032 return TEST_SUCCESS
;
2035 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2037 memcached_return_t rc
;
2040 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2041 test_true(rc
== MEMCACHED_DEPRECATED
);
2043 // Platform dependent
2045 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2049 return TEST_SUCCESS
;
2053 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2055 memcached_return_t rc
;
2059 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2060 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2062 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2064 if (rc
== MEMCACHED_SUCCESS
)
2066 test_true((bool)value
== set
);
2070 test_false((bool)value
== set
);
2073 return TEST_SUCCESS
;
2077 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2079 memcached_return_t rc
;
2083 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2084 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2086 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2088 if (rc
== MEMCACHED_SUCCESS
)
2090 test_true((bool)value
== set
);
2094 test_false((bool)value
== set
);
2097 return TEST_SUCCESS
;
2100 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2102 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2103 char return_key
[MEMCACHED_MAX_KEY
];
2104 size_t return_key_length
;
2106 size_t return_value_length
;
2111 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2112 &return_value_length
, &flags
, &rc
)))
2114 test_true(return_value
);
2115 test_true(rc
== MEMCACHED_SUCCESS
);
2117 *keys_returned
= *keys_returned
+1;
2120 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2122 return TEST_SUCCESS
;
2125 /* Test case provided by Cal Haldenbrand */
2126 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2128 unsigned int setter
= 1;
2130 unsigned long long total
= 0;
2133 char randomstuff
[6 * 1024];
2134 memcached_return_t rc
;
2136 memset(randomstuff
, 0, 6 * 1024);
2138 /* We just keep looking at the same values over and over */
2141 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2142 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2146 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2150 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2151 memset(randomstuff
, 0, 6 * 1024);
2152 test_true(size
< 6 * 1024); /* Being safe here */
2154 for (j
= 0 ; j
< size
;j
++)
2155 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2158 snprintf(key
, sizeof(key
), "%u", x
);
2159 rc
= memcached_set(memc
, key
, strlen(key
),
2160 randomstuff
, strlen(randomstuff
), 10, 0);
2161 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2162 /* If we fail, lets try again */
2163 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2164 rc
= memcached_set(memc
, key
, strlen(key
),
2165 randomstuff
, strlen(randomstuff
), 10, 0);
2166 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2169 return TEST_SUCCESS
;
2172 /* Test case provided by Cal Haldenbrand */
2173 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2175 unsigned int setter
;
2179 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2180 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2182 setter
= 20 * 1024576;
2183 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2184 setter
= 20 * 1024576;
2185 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2186 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2187 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2189 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2192 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2194 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2195 char buffer
[SMALL_STRING_LEN
];
2200 memset(buffer
, 0, SMALL_STRING_LEN
);
2202 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2203 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2204 &val_len
, &flags
, &rc
);
2205 if (rc
!= MEMCACHED_SUCCESS
)
2207 if (rc
== MEMCACHED_NOTFOUND
)
2221 return TEST_SUCCESS
;
2224 /* Do a large mget() over all the keys we think exist */
2225 #define KEY_COUNT 3000 // * 1024576
2226 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2228 memcached_return_t rc
;
2229 unsigned int setter
;
2232 size_t key_lengths
[KEY_COUNT
];
2235 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2236 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2238 setter
= 20 * 1024576;
2239 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2240 setter
= 20 * 1024576;
2241 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2242 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2243 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2246 keys
= static_cast<char **>(calloc(KEY_COUNT
, sizeof(char *)));
2248 for (x
= 0; x
< KEY_COUNT
; x
++)
2252 snprintf(buffer
, 30, "%u", x
);
2253 keys
[x
]= strdup(buffer
);
2254 key_lengths
[x
]= strlen(keys
[x
]);
2257 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2258 test_true(rc
== MEMCACHED_SUCCESS
);
2260 size_t keys_returned
;
2261 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2263 for (x
= 0; x
< KEY_COUNT
; x
++)
2267 return TEST_SUCCESS
;
2270 /* Make sure we behave properly if server list has no values */
2271 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2273 memcached_return_t rc
;
2274 const char *keys
[]= {"fudge", "son", "food"};
2275 size_t key_length
[]= {5, 3, 4};
2278 char return_key
[MEMCACHED_MAX_KEY
];
2279 size_t return_key_length
;
2281 size_t return_value_length
;
2283 /* Here we free everything before running a bunch of mget tests */
2284 memcached_servers_reset(memc
);
2287 /* We need to empty the server before continueing test */
2288 rc
= memcached_flush(memc
, 0);
2289 test_compare(rc
, MEMCACHED_NO_SERVERS
);
2291 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2292 test_true(rc
== MEMCACHED_NO_SERVERS
);
2294 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2295 &return_value_length
, &flags
, &rc
)) != NULL
)
2297 test_true(return_value
);
2299 test_false(return_value
);
2300 test_true(return_value_length
== 0);
2301 test_true(rc
== MEMCACHED_NO_SERVERS
);
2303 for (x
= 0; x
< 3; x
++)
2305 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2306 keys
[x
], key_length
[x
],
2307 (time_t)50, (uint32_t)9);
2308 test_true(rc
== MEMCACHED_NO_SERVERS
);
2311 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2312 test_true(rc
== MEMCACHED_NO_SERVERS
);
2315 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2316 &return_value_length
, &flags
, &rc
)))
2318 test_true(return_value
);
2319 test_true(rc
== MEMCACHED_SUCCESS
);
2320 test_true(return_key_length
== return_value_length
);
2321 test_memcmp(return_value
, return_key
, return_value_length
);
2326 return TEST_SUCCESS
;
2329 #define VALUE_SIZE_BUG5 1048064
2330 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2332 memcached_return_t rc
;
2333 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2334 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2335 char return_key
[MEMCACHED_MAX_KEY
];
2336 size_t return_key_length
;
2338 size_t value_length
;
2342 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2344 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2345 insert_data
[x
]= (signed char)rand();
2347 memcached_flush(memc
, 0);
2348 value
= memcached_get(memc
, keys
[0], key_length
[0],
2349 &value_length
, &flags
, &rc
);
2350 test_true(value
== NULL
);
2351 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2354 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2355 &value_length
, &flags
, &rc
)))
2357 test_true(count
== 0);
2359 for (x
= 0; x
< 4; x
++)
2361 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2362 insert_data
, VALUE_SIZE_BUG5
,
2363 (time_t)0, (uint32_t)0);
2364 test_true(rc
== MEMCACHED_SUCCESS
);
2367 for (x
= 0; x
< 10; x
++)
2369 value
= memcached_get(memc
, keys
[0], key_length
[0],
2370 &value_length
, &flags
, &rc
);
2374 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2376 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2377 &value_length
, &flags
, &rc
)))
2382 test_true(count
== 4);
2384 delete [] insert_data
;
2386 return TEST_SUCCESS
;
2389 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2391 memcached_return_t rc
;
2392 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2393 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2394 char return_key
[MEMCACHED_MAX_KEY
];
2395 size_t return_key_length
;
2397 size_t value_length
;
2401 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2403 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2404 insert_data
[x
]= (signed char)rand();
2406 memcached_flush(memc
, 0);
2407 value
= memcached_get(memc
, keys
[0], key_length
[0],
2408 &value_length
, &flags
, &rc
);
2409 test_true(value
== NULL
);
2410 test_true(rc
== MEMCACHED_NOTFOUND
);
2411 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2412 test_true(rc
== MEMCACHED_SUCCESS
);
2415 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2416 &value_length
, &flags
, &rc
)))
2418 test_true(count
== 0);
2419 test_true(rc
== MEMCACHED_END
);
2421 for (x
= 0; x
< 4; x
++)
2423 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2424 insert_data
, VALUE_SIZE_BUG5
,
2425 (time_t)0, (uint32_t)0);
2426 test_true(rc
== MEMCACHED_SUCCESS
);
2429 for (x
= 0; x
< 2; x
++)
2431 value
= memcached_get(memc
, keys
[0], key_length
[0],
2432 &value_length
, &flags
, &rc
);
2436 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2437 test_true(rc
== MEMCACHED_SUCCESS
);
2439 /* We test for purge of partial complete fetches */
2440 for (count
= 3; count
; count
--)
2442 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2443 &value_length
, &flags
, &rc
);
2444 test_true(rc
== MEMCACHED_SUCCESS
);
2445 test_true(!(memcmp(value
, insert_data
, value_length
)));
2446 test_true(value_length
);
2450 delete [] insert_data
;
2452 return TEST_SUCCESS
;
2455 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2457 memcached_return_t rc
;
2459 memcached_st
*memc_clone
;
2461 memcached_server_st
*servers
;
2462 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";
2465 servers
= memcached_servers_parse(server_list
);
2468 mine
= memcached_create(NULL
);
2469 rc
= memcached_server_push(mine
, servers
);
2470 test_true(rc
== MEMCACHED_SUCCESS
);
2471 memcached_server_list_free(servers
);
2474 memc_clone
= memcached_clone(NULL
, mine
);
2476 memcached_quit(mine
);
2477 memcached_quit(memc_clone
);
2480 memcached_free(mine
);
2481 memcached_free(memc_clone
);
2483 return TEST_SUCCESS
;
2486 /* Test flag store/retrieve */
2487 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2489 const char *keys
= "036790384900";
2490 size_t key_length
= strlen(keys
);
2491 char return_key
[MEMCACHED_MAX_KEY
];
2492 size_t return_key_length
;
2494 size_t value_length
;
2496 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2498 for (unsigned int x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2499 insert_data
[x
]= (signed char)rand();
2501 memcached_flush(memc
, 0);
2504 memcached_return_t rc
= memcached_set(memc
, keys
, key_length
,
2505 insert_data
, VALUE_SIZE_BUG5
,
2507 test_true(rc
== MEMCACHED_SUCCESS
);
2510 value
= memcached_get(memc
, keys
, key_length
,
2511 &value_length
, &flags
, &rc
);
2512 test_true(flags
== 245);
2516 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2519 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2520 &value_length
, &flags
, &rc
);
2521 test_compare(245, flags
);
2524 delete [] insert_data
;
2527 return TEST_SUCCESS
;
2530 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2532 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2533 size_t key_length
[3];
2537 char return_key
[MEMCACHED_MAX_KEY
];
2538 size_t return_key_length
;
2540 size_t return_value_length
;
2543 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2544 key_length
[1]= strlen("fudge&*@#");
2545 key_length
[2]= strlen("for^#@&$not");
2548 for (unsigned int x
= 0; x
< 3; x
++)
2550 memcached_return_t rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2551 keys
[x
], key_length
[x
],
2552 (time_t)50, (uint32_t)9);
2553 test_true(rc
== MEMCACHED_SUCCESS
);
2556 memcached_return_t rc
= memcached_mget(memc
, keys
, key_length
, 3);
2557 test_true(rc
== MEMCACHED_SUCCESS
);
2559 /* We need to empty the server before continueing test */
2560 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2561 &return_value_length
, &flags
, &rc
)) != NULL
)
2563 test_true(return_value
);
2567 test_compare(3, count
);
2569 return TEST_SUCCESS
;
2572 /* We are testing with aggressive timeout to get failures */
2573 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2575 const char *key
= "foo";
2577 size_t value_length
= 512;
2580 unsigned int set
= 1;
2581 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2584 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2585 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2587 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2590 value
= (char*)malloc(value_length
* sizeof(char));
2592 for (x
= 0; x
< value_length
; x
++)
2593 value
[x
]= (char) (x
% 127);
2595 for (x
= 1; x
<= 100000; ++x
)
2597 memcached_return_t rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2599 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2600 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2602 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2607 memcached_free(mclone
);
2609 return TEST_SUCCESS
;
2613 We are looking failures in the async protocol
2615 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2617 const char *key
= "foo";
2619 size_t value_length
= 512;
2622 memcached_return_t rc
;
2623 unsigned int set
= 1;
2625 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2627 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2628 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2630 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2633 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2635 test_true(timeout
== -1);
2637 value
= (char*)malloc(value_length
* sizeof(char));
2639 for (x
= 0; x
< value_length
; x
++)
2640 value
[x
]= (char) (x
% 127);
2642 for (x
= 1; x
<= 100000; ++x
)
2644 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2648 memcached_free(mclone
);
2650 return TEST_SUCCESS
;
2654 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2656 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2658 memcached_return_t rc
;
2660 size_t value_length
;
2662 uint64_t number_value
;
2664 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2665 &value_length
, &flags
, &rc
);
2666 test_true(value
== NULL
);
2667 test_true(rc
== MEMCACHED_NOTFOUND
);
2669 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2672 test_true(value
== NULL
);
2673 /* The binary protocol will set the key if it doesn't exist */
2674 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2676 test_true(rc
== MEMCACHED_SUCCESS
);
2680 test_true(rc
== MEMCACHED_NOTFOUND
);
2683 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2685 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2686 &value_length
, &flags
, &rc
);
2688 test_true(rc
== MEMCACHED_SUCCESS
);
2691 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2693 test_true(number_value
== 2);
2694 test_true(rc
== MEMCACHED_SUCCESS
);
2696 return TEST_SUCCESS
;
2700 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2701 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2703 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2705 char key
[] = "key34567890";
2706 memcached_return_t rc
;
2707 size_t overflowSize
;
2709 char commandFirst
[]= "set key34567890 0 0 ";
2710 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2711 size_t commandLength
;
2714 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2716 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2718 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2720 char *overflow
= new (std::nothrow
) char[testSize
];
2721 test_true(overflow
);
2723 memset(overflow
, 'x', testSize
);
2724 rc
= memcached_set(memc
, key
, strlen(key
),
2725 overflow
, testSize
, 0, 0);
2726 test_true(rc
== MEMCACHED_SUCCESS
);
2730 return TEST_SUCCESS
;
2735 Test values of many different sizes
2736 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2737 set key34567890 0 0 8169 \r\n
2738 is sent followed by buffer of size 8169, followed by 8169
2740 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2744 memcached_return_t rc
;
2745 const char *key
= "foo";
2747 size_t value_length
= 18000;
2749 size_t string_length
;
2752 size_t current_length
;
2754 value
= (char*)malloc(value_length
);
2757 for (x
= 0; x
< value_length
; x
++)
2758 value
[x
] = (char) (x
% 127);
2760 for (current_length
= 0; current_length
< value_length
; current_length
++)
2762 rc
= memcached_set(memc
, key
, strlen(key
),
2763 value
, current_length
,
2764 (time_t)0, (uint32_t)0);
2765 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2767 string
= memcached_get(memc
, key
, strlen(key
),
2768 &string_length
, &flags
, &rc
);
2770 test_true(rc
== MEMCACHED_SUCCESS
);
2771 test_true(string_length
== current_length
);
2772 test_true(!memcmp(string
, value
, string_length
));
2779 return TEST_SUCCESS
;
2783 Look for zero length value problems
2785 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2788 memcached_return_t rc
;
2789 const char *key
= "mykey";
2794 for (x
= 0; x
< 2; x
++)
2796 rc
= memcached_set(memc
, key
, strlen(key
),
2798 (time_t)0, (uint32_t)0);
2800 test_true(rc
== MEMCACHED_SUCCESS
);
2802 value
= memcached_get(memc
, key
, strlen(key
),
2803 &length
, &flags
, &rc
);
2805 test_true(rc
== MEMCACHED_SUCCESS
);
2806 test_true(value
== NULL
);
2807 test_true(length
== 0);
2808 test_true(flags
== 0);
2810 value
= memcached_get(memc
, key
, strlen(key
),
2811 &length
, &flags
, &rc
);
2813 test_true(rc
== MEMCACHED_SUCCESS
);
2814 test_true(value
== NULL
);
2815 test_true(length
== 0);
2816 test_true(flags
== 0);
2819 return TEST_SUCCESS
;
2822 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2823 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2825 memcached_return_t rc
;
2826 const char *key
= "mykey";
2831 rc
= memcached_set(memc
, key
, strlen(key
),
2833 (time_t)0, UINT32_MAX
);
2835 test_true(rc
== MEMCACHED_SUCCESS
);
2837 value
= memcached_get(memc
, key
, strlen(key
),
2838 &length
, &flags
, &rc
);
2840 test_true(rc
== MEMCACHED_SUCCESS
);
2841 test_true(value
== NULL
);
2842 test_true(length
== 0);
2843 test_true(flags
== UINT32_MAX
);
2845 return TEST_SUCCESS
;
2848 #if !defined(__sun) && !defined(__OpenBSD__)
2849 /* Check the validity of chinese key*/
2850 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2852 memcached_return_t rc
;
2853 const char *key
= "豆瓣";
2854 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2859 rc
= memcached_set(memc
, key
, strlen(key
),
2860 value
, strlen(value
),
2863 test_true(rc
== MEMCACHED_SUCCESS
);
2865 value2
= memcached_get(memc
, key
, strlen(key
),
2866 &length
, &flags
, &rc
);
2868 test_true(length
==strlen(value
));
2869 test_true(rc
== MEMCACHED_SUCCESS
);
2870 test_true(memcmp(value
, value2
, length
)==0);
2873 return TEST_SUCCESS
;
2881 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2884 const memcached_server_st
*server
;
2885 memcached_return_t res
;
2889 memc
= memcached_create(NULL
);
2890 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2891 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2893 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2895 memcached_free(memc
);
2897 return TEST_SUCCESS
;
2900 /* CAS test from Andei */
2901 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2903 memcached_return_t status
;
2904 memcached_result_st
*result
, result_obj
;
2905 const char *key
= "abc";
2906 size_t key_len
= strlen("abc");
2907 const char *value
= "foobar";
2908 size_t value_len
= strlen(value
);
2910 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2912 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2913 test_true(status
== MEMCACHED_SUCCESS
);
2915 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2916 test_true(status
== MEMCACHED_SUCCESS
);
2918 result
= memcached_result_create(memc
, &result_obj
);
2921 memcached_result_create(memc
, &result_obj
);
2922 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2925 test_true(status
== MEMCACHED_SUCCESS
);
2927 memcached_result_free(result
);
2929 return TEST_SUCCESS
;
2932 #include "ketama_test_cases.h"
2933 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2935 memcached_return_t rc
;
2938 memcached_server_st
*server_pool
;
2943 memc
= memcached_create(NULL
);
2946 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2947 test_true(rc
== MEMCACHED_SUCCESS
);
2949 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2950 test_true(value
== 1);
2952 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2953 test_true(rc
== MEMCACHED_SUCCESS
);
2955 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2956 test_true(value
== MEMCACHED_HASH_MD5
);
2958 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");
2959 memcached_server_push(memc
, server_pool
);
2961 /* verify that the server list was parsed okay. */
2962 test_true(memcached_server_count(memc
) == 8);
2963 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2964 test_true(server_pool
[0].port
== 11211);
2965 test_true(server_pool
[0].weight
== 600);
2966 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2967 test_true(server_pool
[2].port
== 11211);
2968 test_true(server_pool
[2].weight
== 200);
2969 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2970 test_true(server_pool
[7].port
== 11211);
2971 test_true(server_pool
[7].weight
== 100);
2973 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2974 * us test the boundary wraparound.
2976 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
2978 /* verify the standard ketama set. */
2979 for (x
= 0; x
< 99; x
++)
2981 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2983 memcached_server_instance_st instance
=
2984 memcached_server_instance_by_position(memc
, server_idx
);
2986 const char *hostname
= memcached_server_name(instance
);
2987 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2990 memcached_server_list_free(server_pool
);
2991 memcached_free(memc
);
2993 return TEST_SUCCESS
;
2996 /* Large mget() of missing keys with binary proto
2998 * If many binary quiet commands (such as getq's in an mget) fill the output
2999 * buffer and the server chooses not to respond, memcached_flush hangs. See
3000 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
3003 /* sighandler_t function that always asserts false */
3004 static void fail(int unused
)
3011 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
3016 return TEST_SKIPPED
;
3018 void (*oldalarm
)(int);
3020 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3021 test_true(memc_clone
);
3023 /* only binproto uses getq for mget */
3024 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3026 /* empty the cache to ensure misses (hence non-responses) */
3027 memcached_return_t rc
= memcached_flush(memc_clone
, 0);
3028 test_true(rc
== MEMCACHED_SUCCESS
);
3030 size_t* key_lengths
= new (std::nothrow
) size_t[key_count
];
3031 test_true(key_lengths
);
3032 char **keys
= static_cast<char **>(calloc(key_count
, sizeof(char *)));
3034 for (unsigned int x
= 0; x
< key_count
; x
++)
3038 snprintf(buffer
, 30, "%u", x
);
3039 keys
[x
]= strdup(buffer
);
3040 key_lengths
[x
]= strlen(keys
[x
]);
3043 oldalarm
= signal(SIGALRM
, fail
);
3046 test_true_got(memcached_success(memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
)), memcached_last_error_message(memc_clone
));
3049 signal(SIGALRM
, oldalarm
);
3051 size_t keys_returned
;
3052 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3054 for (unsigned int x
= 0; x
< key_count
; x
++)
3059 delete [] key_lengths
;
3061 memcached_free(memc_clone
);
3063 return TEST_SUCCESS
;
3067 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3069 test_return_t test_rc
;
3070 test_rc
= pre_binary(memc
);
3072 if (test_rc
!= TEST_SUCCESS
)
3077 /* should work as of r580 */
3078 rc
= _user_supplied_bug21(memc
, 10);
3079 test_true(rc
== TEST_SUCCESS
);
3081 /* should fail as of r580 */
3082 rc
= _user_supplied_bug21(memc
, 1000);
3083 test_true(rc
== TEST_SUCCESS
);
3085 return TEST_SUCCESS
;
3088 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3091 memcached_server_instance_st instance
;
3093 memcached_return_t rc
;
3094 memcached_st
*memc
= memcached_create(NULL
);
3097 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3098 test_true(rc
== MEMCACHED_SUCCESS
);
3100 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3101 test_true(value
== 1);
3103 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3104 test_true(rc
== MEMCACHED_SUCCESS
);
3106 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3107 test_true(value
== MEMCACHED_HASH_MD5
);
3109 /* server should be removed when in delay */
3110 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3111 test_true(rc
== MEMCACHED_SUCCESS
);
3113 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3114 test_true(value
== 1);
3116 memcached_server_st
*server_pool
;
3117 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");
3118 memcached_server_push(memc
, server_pool
);
3120 /* verify that the server list was parsed okay. */
3121 test_true(memcached_server_count(memc
) == 8);
3122 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3123 test_true(server_pool
[0].port
== 11211);
3124 test_true(server_pool
[0].weight
== 600);
3125 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3126 test_true(server_pool
[2].port
== 11211);
3127 test_true(server_pool
[2].weight
== 200);
3128 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3129 test_true(server_pool
[7].port
== 11211);
3130 test_true(server_pool
[7].weight
== 100);
3132 instance
= memcached_server_instance_by_position(memc
, 2);
3133 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3134 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3137 This would not work if there were only two hosts.
3139 for (size_t x
= 0; x
< 99; x
++)
3141 memcached_autoeject(memc
);
3142 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3143 test_true(server_idx
!= 2);
3146 /* and re-added when it's back. */
3147 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3148 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3149 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3150 memc
->distribution
);
3151 for (size_t x
= 0; x
< 99; x
++)
3153 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3154 // We re-use instance from above.
3156 memcached_server_instance_by_position(memc
, server_idx
);
3157 const char *hostname
= memcached_server_name(instance
);
3158 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3161 memcached_server_list_free(server_pool
);
3162 memcached_free(memc
);
3164 return TEST_SUCCESS
;
3167 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3171 memcached_return_t rc
;
3172 memcached_st
*memc
= memcached_create(NULL
);
3176 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3177 test_true(rc
== MEMCACHED_SUCCESS
);
3179 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3180 test_true(value
== 1);
3182 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3183 test_true(rc
== MEMCACHED_SUCCESS
);
3185 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3186 test_true(value
== MEMCACHED_HASH_MD5
);
3189 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3191 memcached_server_st
*server_pool
;
3192 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");
3193 memcached_server_push(memc
, server_pool
);
3195 // @todo this needs to be refactored to actually test something.
3198 if ((fp
= fopen("ketama_keys.txt", "w")))
3202 printf("cannot write to file ketama_keys.txt");
3203 return TEST_FAILURE
;
3206 for (int x
= 0; x
< 10000; x
++)
3209 snprintf(key
, sizeof(key
), "%d", x
);
3211 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3212 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3213 in_port_t port
= memc
->hosts
[server_idx
].port
;
3214 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3215 memcached_server_instance_st instance
=
3216 memcached_server_instance_by_position(memc
, host_index
);
3220 memcached_server_list_free(server_pool
);
3221 memcached_free(memc
);
3223 return TEST_SUCCESS
;
3227 static test_return_t
result_static(memcached_st
*memc
)
3229 memcached_result_st result
;
3230 memcached_result_st
*result_ptr
;
3232 result_ptr
= memcached_result_create(memc
, &result
);
3233 test_true(result
.options
.is_allocated
== false);
3234 test_true(memcached_is_initialized(&result
) == true);
3235 test_true(result_ptr
);
3236 test_true(result_ptr
== &result
);
3238 memcached_result_free(&result
);
3240 test_true(result
.options
.is_allocated
== false);
3241 test_true(memcached_is_initialized(&result
) == false);
3243 return TEST_SUCCESS
;
3246 static test_return_t
result_alloc(memcached_st
*memc
)
3248 memcached_result_st
*result_ptr
;
3250 result_ptr
= memcached_result_create(memc
, NULL
);
3251 test_true(result_ptr
);
3252 test_true(result_ptr
->options
.is_allocated
== true);
3253 test_true(memcached_is_initialized(result_ptr
) == true);
3254 memcached_result_free(result_ptr
);
3256 return TEST_SUCCESS
;
3259 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3262 pairs_free(global_pairs
);
3264 return TEST_SUCCESS
;
3267 static test_return_t
generate_pairs(memcached_st
*memc
)
3270 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3271 global_count
= GLOBAL_COUNT
;
3273 for (size_t x
= 0; x
< global_count
; x
++)
3275 global_keys
[x
]= global_pairs
[x
].key
;
3276 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3279 return TEST_SUCCESS
;
3282 static test_return_t
generate_large_pairs(memcached_st
*)
3284 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3285 global_count
= GLOBAL2_COUNT
;
3287 for (size_t x
= 0; x
< global_count
; x
++)
3289 global_keys
[x
]= global_pairs
[x
].key
;
3290 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3293 return TEST_SUCCESS
;
3296 static test_return_t
generate_data(memcached_st
*memc
)
3298 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3300 test_true(check_execute
== global_count
);
3302 return TEST_SUCCESS
;
3305 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3307 uint32_t host_index
= 0;
3308 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3310 test_true(check_execute
== global_count
);
3312 // @todo hosts used size stats
3313 memcached_return_t rc
;
3314 memcached_stat_st
*stat_p
= memcached_stat(memc
, NULL
, &rc
);
3317 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3319 /* This test was changes so that "make test" would work properlly */
3321 memcached_server_instance_st instance
=
3322 memcached_server_instance_by_position(memc
, host_index
);
3324 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3326 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3329 memcached_stat_free(NULL
, stat_p
);
3331 return TEST_SUCCESS
;
3333 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3338 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3339 generate_data(memc
);
3341 return TEST_SUCCESS
;
3344 static test_return_t
get_read_count(memcached_st
*memc
)
3346 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3347 test_true(memc_clone
);
3349 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3353 size_t return_value_length
;
3357 for (size_t x
= count
= 0; x
< global_count
; x
++)
3359 memcached_return_t rc
;
3360 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3361 &return_value_length
, &flags
, &rc
);
3362 if (rc
== MEMCACHED_SUCCESS
)
3371 memcached_free(memc_clone
);
3373 return TEST_SUCCESS
;
3376 static test_return_t
get_read(memcached_st
*memc
)
3378 for (size_t x
= 0; x
< global_count
; x
++)
3380 size_t return_value_length
;
3382 memcached_return_t rc
;
3383 char *return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3384 &return_value_length
, &flags
, &rc
);
3386 test_true(return_value);
3387 test_true(rc == MEMCACHED_SUCCESS);
3389 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3393 return TEST_SUCCESS
;
3396 static test_return_t
mget_read(memcached_st
*memc
)
3399 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3400 return TEST_SKIPPED
;
3402 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3404 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3406 // Go fetch the keys and test to see if all of them were returned
3408 size_t keys_returned
;
3409 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3411 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3412 test_true_got(global_count
== keys_returned
, buffer
);
3416 return TEST_SUCCESS
;
3419 static test_return_t
mget_read_result(memcached_st
*memc
)
3422 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3423 return TEST_SKIPPED
;
3425 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3427 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3429 /* Turn this into a help function */
3431 memcached_result_st results_obj
;
3432 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
3434 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3437 test_true(rc
== MEMCACHED_SUCCESS
);
3440 memcached_result_free(&results_obj
);
3443 return TEST_SUCCESS
;
3446 static test_return_t
mget_read_function(memcached_st
*memc
)
3449 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3450 return TEST_SKIPPED
;
3452 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3454 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3456 memcached_execute_fn callbacks
[]= { &callback_counter
};
3458 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3460 return TEST_SUCCESS
;
3463 static test_return_t
delete_generate(memcached_st
*memc
)
3465 for (size_t x
= 0; x
< global_count
; x
++)
3467 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3470 return TEST_SUCCESS
;
3473 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3476 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3478 for (size_t x
= 0; x
< global_count
; x
++)
3480 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3483 return TEST_SUCCESS
;
3486 static test_return_t
add_host_test1(memcached_st
*memc
)
3488 memcached_return_t rc
;
3489 char servername
[]= "0.example.com";
3491 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3493 test_compare(1, memcached_server_list_count(servers
));
3495 for (size_t x
= 2; x
< 20; x
++)
3497 char buffer
[SMALL_STRING_LEN
];
3499 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3500 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3502 test_compare(MEMCACHED_SUCCESS
, rc
);
3503 test_compare(x
, memcached_server_list_count(servers
));
3506 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3507 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3509 memcached_server_list_free(servers
);
3511 return TEST_SUCCESS
;
3514 static test_return_t
pre_nonblock(memcached_st
*memc
)
3516 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3518 return TEST_SUCCESS
;
3521 static test_return_t
pre_cork(memcached_st
*memc
)
3524 return TEST_SKIPPED
;
3527 if (memcached_success(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
)))
3528 return TEST_SUCCESS
;
3530 return TEST_SKIPPED
;
3533 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3536 return TEST_SKIPPED
;
3538 test_return_t test_rc
;
3539 if ((test_rc
= pre_cork(memc
)) != TEST_SUCCESS
)
3542 return pre_nonblock(memc
);
3545 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3547 memcached_return_t rc
= MEMCACHED_FAILURE
;
3548 memcached_st
*memc_clone
;
3550 memc_clone
= memcached_clone(NULL
, memc
);
3551 test_true(memc_clone
);
3552 // The memcached_version needs to be done on a clone, because the server
3553 // will not toggle protocol on an connection.
3554 memcached_version(memc_clone
);
3556 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3558 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3559 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3560 test_true(rc
== MEMCACHED_SUCCESS
);
3561 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3565 return TEST_SKIPPED
;
3568 memcached_free(memc_clone
);
3570 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3573 static test_return_t
pre_murmur(memcached_st
*memc
)
3575 #ifdef HAVE_MURMUR_HASH
3576 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3577 return TEST_SUCCESS
;
3580 return TEST_SKIPPED
;
3584 static test_return_t
pre_jenkins(memcached_st
*memc
)
3586 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3588 return TEST_SUCCESS
;
3592 static test_return_t
pre_md5(memcached_st
*memc
)
3594 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3596 return TEST_SUCCESS
;
3599 static test_return_t
pre_crc(memcached_st
*memc
)
3601 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3603 return TEST_SUCCESS
;
3606 static test_return_t
pre_hsieh(memcached_st
*memc
)
3608 #ifdef HAVE_HSIEH_HASH
3609 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3610 return TEST_SUCCESS
;
3613 return TEST_SKIPPED
;
3617 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3619 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3621 return TEST_SUCCESS
;
3624 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3628 return TEST_SUCCESS
;
3631 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3633 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3635 return TEST_SUCCESS
;
3638 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3640 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3642 return TEST_SUCCESS
;
3645 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3647 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3648 test_true(rc
== MEMCACHED_SUCCESS
);
3650 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3651 test_true(value
== 1);
3653 return TEST_SUCCESS
;
3656 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3658 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3659 test_true(rc
== MEMCACHED_SUCCESS
);
3661 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3662 test_true(value
== 1);
3664 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3665 test_true(rc
== MEMCACHED_SUCCESS
);
3667 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3668 test_true(value
== MEMCACHED_HASH_MD5
);
3670 return TEST_SUCCESS
;
3674 @note This should be testing to see if the server really supports the binary protocol.
3676 static test_return_t
pre_binary(memcached_st
*memc
)
3678 memcached_return_t rc
= MEMCACHED_FAILURE
;
3680 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3682 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3683 test_true(rc
== MEMCACHED_SUCCESS
);
3684 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3687 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3690 static test_return_t
pre_sasl(memcached_st
*memc
)
3692 memcached_return_t rc
= MEMCACHED_FAILURE
;
3694 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3695 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3696 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3697 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3699 if (server
and user
and pass
)
3701 memcached_server_st
*servers
= memcached_servers_parse(server
);
3703 memcached_servers_reset(memc
);
3704 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3705 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3706 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3707 test_true(rc
== MEMCACHED_SUCCESS
);
3713 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3716 static test_return_t
pre_replication(memcached_st
*memc
)
3718 test_true(TEST_SUCCESS
== pre_binary(memc
));
3721 * Make sure that we store the item on all servers
3722 * (master + replicas == number of servers)
3724 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3725 memcached_server_count(memc
) - 1);
3726 test_true(rc
== MEMCACHED_SUCCESS
);
3727 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3729 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3733 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3735 test_compare(TEST_SUCCESS
, pre_replication(memc
));
3737 return pre_nonblock(memc
);
3741 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3745 #ifdef HARD_MALLOC_TESTS
3746 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3754 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3758 #ifdef HARD_MALLOC_TESTS
3759 void *ret
= malloc(size
+ 8);
3762 ret
= (void*)((caddr_t
)ret
+ 8);
3765 void *ret
= malloc(size
);
3770 memset(ret
, 0xff, size
);
3777 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *)
3779 #ifdef HARD_MALLOC_TESTS
3780 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3781 void *nmem
= realloc(real_ptr
, size
+ 8);
3786 ret
= (void*)((caddr_t
)nmem
+ 8);
3792 return realloc(mem
, size
);
3797 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *)
3799 #ifdef HARD_MALLOC_TESTS
3800 void *mem
= my_malloc(ptr
, nelem
* size
);
3803 memset(mem
, 0, nelem
* size
);
3809 return calloc(nelem
, size
);
3813 static test_return_t
set_prefix(memcached_st
*memc
)
3815 memcached_return_t rc
;
3816 const char *key
= "mine";
3819 /* Make sure be default none exists */
3820 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3821 test_true(rc
== MEMCACHED_FAILURE
);
3823 /* Test a clean set */
3824 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3825 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3827 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3829 test_true(memcmp(value
, key
, 4) == 0);
3830 test_true(rc
== MEMCACHED_SUCCESS
);
3832 /* Test that we can turn it off */
3833 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3834 test_true(rc
== MEMCACHED_SUCCESS
);
3836 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3838 test_true(rc
== MEMCACHED_FAILURE
);
3840 /* Now setup for main test */
3841 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3842 test_true(rc
== MEMCACHED_SUCCESS
);
3844 value
= (char *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3846 test_true(rc
== MEMCACHED_SUCCESS
);
3847 test_true(memcmp(value
, key
, 4) == 0);
3849 /* Set to Zero, and then Set to something too large */
3852 memset(long_key
, 0, 255);
3854 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3855 test_true(rc
== MEMCACHED_SUCCESS
);
3857 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3859 test_true(rc
== MEMCACHED_FAILURE
);
3860 test_true(value
== NULL
);
3862 /* Test a long key for failure */
3863 /* TODO, extend test to determine based on setting, what result should be */
3864 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3865 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3866 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3867 test_true(rc
== MEMCACHED_SUCCESS
);
3869 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3870 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3871 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3872 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3874 /* Test for a bad prefix, but with a short key */
3875 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3876 test_true(rc
== MEMCACHED_SUCCESS
);
3878 strncpy(long_key
, "dog cat", sizeof(long_key
));
3879 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3880 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3883 return TEST_SUCCESS
;
3887 #ifdef MEMCACHED_ENABLE_DEPRECATED
3888 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3890 void *test_ptr
= NULL
;
3893 memcached_malloc_fn malloc_cb
=
3894 (memcached_malloc_fn
)my_malloc
;
3895 cb_ptr
= *(void **)&malloc_cb
;
3896 memcached_return_t rc
;
3898 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3899 test_true(rc
== MEMCACHED_SUCCESS
);
3900 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3901 test_true(rc
== MEMCACHED_SUCCESS
);
3902 test_true(test_ptr
== cb_ptr
);
3906 memcached_realloc_fn realloc_cb
=
3907 (memcached_realloc_fn
)my_realloc
;
3908 cb_ptr
= *(void **)&realloc_cb
;
3909 memcached_return_t rc
;
3911 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3912 test_true(rc
== MEMCACHED_SUCCESS
);
3913 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3914 test_true(rc
== MEMCACHED_SUCCESS
);
3915 test_true(test_ptr
== cb_ptr
);
3919 memcached_free_fn free_cb
=
3920 (memcached_free_fn
)my_free
;
3921 cb_ptr
= *(void **)&free_cb
;
3922 memcached_return_t rc
;
3924 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3925 test_true(rc
== MEMCACHED_SUCCESS
);
3926 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3927 test_true(rc
== MEMCACHED_SUCCESS
);
3928 test_true(test_ptr
== cb_ptr
);
3931 return TEST_SUCCESS
;
3936 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3938 memcached_return_t rc
;
3939 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3940 my_realloc
, my_calloc
, NULL
);
3941 test_true(rc
== MEMCACHED_FAILURE
);
3943 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3944 my_realloc
, my_calloc
, NULL
);
3946 memcached_malloc_fn mem_malloc
;
3947 memcached_free_fn mem_free
;
3948 memcached_realloc_fn mem_realloc
;
3949 memcached_calloc_fn mem_calloc
;
3950 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3951 &mem_realloc
, &mem_calloc
);
3953 test_true(mem_malloc
== my_malloc
);
3954 test_true(mem_realloc
== my_realloc
);
3955 test_true(mem_calloc
== my_calloc
);
3956 test_true(mem_free
== my_free
);
3958 return TEST_SUCCESS
;
3961 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3964 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3965 memcached_hash_t hash
;
3966 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3967 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3970 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3971 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3973 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3975 if (hash
!= MEMCACHED_HASH_CRC
)
3976 return TEST_SKIPPED
;
3978 return TEST_SUCCESS
;
3981 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3984 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3985 memcached_hash_t hash
;
3986 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3987 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3990 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3991 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3993 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3995 if (hash
!= MEMCACHED_HASH_HSIEH
)
3996 return TEST_SKIPPED
;
3999 return TEST_SUCCESS
;
4002 static test_return_t
enable_cas(memcached_st
*memc
)
4004 unsigned int set
= 1;
4006 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4008 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4010 return TEST_SUCCESS
;
4013 return TEST_SKIPPED
;
4016 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4018 memcached_version(memc
);
4020 memcached_server_instance_st instance
=
4021 memcached_server_instance_by_position(memc
, 0);
4023 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4024 || instance
->minor_version
> 2)
4026 return TEST_SUCCESS
;
4029 return TEST_SKIPPED
;
4032 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4034 memcached_return_t rc
;
4037 memcached_servers_reset(memc
);
4039 if (stat("/tmp/memcached.socket", &buf
))
4040 return TEST_SKIPPED
;
4042 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4044 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4047 static test_return_t
pre_nodelay(memcached_st
*memc
)
4049 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4050 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4052 return TEST_SUCCESS
;
4055 static test_return_t
pre_settimer(memcached_st
*memc
)
4057 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4058 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4060 return TEST_SUCCESS
;
4063 static test_return_t
poll_timeout(memcached_st
*memc
)
4069 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4071 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4073 test_true(timeout
== 100);
4075 return TEST_SUCCESS
;
4078 static test_return_t
noreply_test(memcached_st
*memc
)
4080 memcached_return_t ret
;
4081 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4082 test_true(ret
== MEMCACHED_SUCCESS
);
4083 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4084 test_true(ret
== MEMCACHED_SUCCESS
);
4085 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4086 test_true(ret
== MEMCACHED_SUCCESS
);
4087 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4088 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4089 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4091 for (int count
=0; count
< 5; ++count
)
4093 for (size_t x
= 0; x
< 100; ++x
)
4096 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4097 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4099 size_t len
= (size_t)check_length
;
4104 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4107 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4110 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4113 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4116 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4122 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4126 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4127 ** API and is _ONLY_ done this way to verify that the library works the
4128 ** way it is supposed to do!!!!
4131 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4133 memcached_server_instance_st instance
=
4134 memcached_server_instance_by_position(memc
, x
);
4135 no_msg
+=(int)(instance
->cursor_active
);
4138 test_true(no_msg
== 0);
4139 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4142 ** Now validate that all items was set properly!
4144 for (size_t x
= 0; x
< 100; ++x
)
4148 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4150 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4152 size_t len
= (size_t)check_length
;
4155 char* value
=memcached_get(memc
, key
, strlen(key
),
4156 &length
, &flags
, &ret
);
4157 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4160 case 0: /* FALLTHROUGH */
4161 case 1: /* FALLTHROUGH */
4163 test_true(strncmp(value
, key
, len
) == 0);
4164 test_true(len
== length
);
4167 test_true(length
== len
* 2);
4170 test_true(length
== len
* 3);
4180 /* Try setting an illegal cas value (should not return an error to
4181 * the caller (because we don't expect a return message from the server)
4183 const char* keys
[]= {"0"};
4184 size_t lengths
[]= {1};
4187 memcached_result_st results_obj
;
4188 memcached_result_st
*results
;
4189 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4190 test_true(ret
== MEMCACHED_SUCCESS
);
4192 results
= memcached_result_create(memc
, &results_obj
);
4194 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4196 test_true(ret
== MEMCACHED_SUCCESS
);
4197 uint64_t cas
= memcached_result_cas(results
);
4198 memcached_result_free(&results_obj
);
4200 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4201 test_true(ret
== MEMCACHED_SUCCESS
);
4204 * The item will have a new cas value, so try to set it again with the old
4205 * value. This should fail!
4207 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4208 test_true(ret
== MEMCACHED_SUCCESS
);
4209 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4210 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4211 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4214 return TEST_SUCCESS
;
4217 static test_return_t
analyzer_test(memcached_st
*memc
)
4219 memcached_return_t rc
;
4220 memcached_stat_st
*memc_stat
;
4221 memcached_analysis_st
*report
;
4223 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4224 test_true(rc
== MEMCACHED_SUCCESS
);
4225 test_true(memc_stat
);
4227 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4228 test_true(rc
== MEMCACHED_SUCCESS
);
4232 memcached_stat_free(NULL
, memc_stat
);
4234 return TEST_SUCCESS
;
4237 /* Count the objects */
4238 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4243 (void)ptr
; (void)key
; (void)key_length
;
4244 size_t *counter
= (size_t *)context
;
4246 *counter
= *counter
+ 1;
4248 return MEMCACHED_SUCCESS
;
4251 static test_return_t
dump_test(memcached_st
*memc
)
4254 memcached_dump_fn callbacks
[1];
4255 test_return_t main_rc
;
4257 callbacks
[0]= &callback_dump_counter
;
4259 /* No support for Binary protocol yet */
4260 if (memc
->flags
.binary_protocol
)
4261 return TEST_SUCCESS
;
4263 main_rc
= set_test3(memc
);
4265 test_true (main_rc
== TEST_SUCCESS
);
4267 memcached_return_t rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4268 test_true(rc
== MEMCACHED_SUCCESS
);
4270 /* We may have more then 32 if our previous flush has not completed */
4271 test_true(counter
>= 32);
4273 return TEST_SUCCESS
;
4276 #ifdef HAVE_LIBMEMCACHEDUTIL
4278 struct test_pool_context_st
{
4279 memcached_pool_st
* pool
;
4283 static void* connection_release(void *arg
)
4285 test_pool_context_st
*resource
= static_cast<test_pool_context_st
*>(arg
);
4288 // Release all of the memc we are holding
4289 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4293 #define POOL_SIZE 10
4294 static test_return_t
connection_pool_test(memcached_st
*memc
)
4296 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4297 test_true(pool
!= NULL
);
4298 memcached_st
*mmc
[POOL_SIZE
];
4299 memcached_return_t rc
;
4301 // Fill up our array that we will store the memc that are in the pool
4302 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4304 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4305 test_true(mmc
[x
] != NULL
);
4306 test_true(rc
== MEMCACHED_SUCCESS
);
4309 // All memc should be gone
4310 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4311 test_true(rc
== MEMCACHED_SUCCESS
);
4314 test_pool_context_st item
= { pool
, mmc
[9] };
4316 pthread_create(&tid
, NULL
, connection_release
, &item
);
4317 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4318 test_true(rc
== MEMCACHED_SUCCESS
);
4319 pthread_join(tid
, NULL
);
4321 const char *key
= "key";
4322 size_t keylen
= strlen(key
);
4324 // verify that I can do ops with all connections
4325 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4326 test_true(rc
== MEMCACHED_SUCCESS
);
4328 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4330 uint64_t number_value
;
4331 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4332 test_true(rc
== MEMCACHED_SUCCESS
);
4333 test_true(number_value
== (x
+1));
4337 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4339 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4343 /* verify that I can set behaviors on the pool when I don't have all
4344 * of the connections in the pool. It should however be enabled
4345 * when I push the item into the pool
4347 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4348 test_true(mmc
[0] != NULL
);
4350 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4351 test_true(rc
== MEMCACHED_SUCCESS
);
4353 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4354 test_true(mmc
[1] != NULL
);
4356 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4357 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4358 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4360 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4361 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4362 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4364 test_true(memcached_pool_destroy(pool
) == memc
);
4366 return TEST_SUCCESS
;
4369 static test_return_t
util_version_test(memcached_st
*memc
)
4373 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4374 test_true(if_successful
== true);
4376 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4378 // We expect failure
4381 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4382 fprintf(stderr
, "\nDumping Server Information\n\n");
4383 memcached_server_fn callbacks
[1];
4385 callbacks
[0]= dump_server_information
;
4386 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4387 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4389 test_true(if_successful
== false);
4391 memcached_server_instance_st instance
=
4392 memcached_server_instance_by_position(memc
, 0);
4394 memcached_version(memc
);
4396 // We only use one binary when we test, so this should be just fine.
4397 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4398 test_true(if_successful
== true);
4400 if (instance
->micro_version
> 0)
4401 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4402 else if (instance
->minor_version
> 0)
4403 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4404 else if (instance
->major_version
> 0)
4405 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4407 test_true(if_successful
== true);
4409 if (instance
->micro_version
> 0)
4410 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4411 else if (instance
->minor_version
> 0)
4412 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4413 else if (instance
->major_version
> 0)
4414 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4416 test_true(if_successful
== false);
4418 return TEST_SUCCESS
;
4421 static test_return_t
ping_test(memcached_st
*memc
)
4423 memcached_return_t rc
;
4424 memcached_server_instance_st instance
=
4425 memcached_server_instance_by_position(memc
, 0);
4427 // Test both the version that returns a code, and the one that does not.
4428 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4429 memcached_server_port(instance
), NULL
));
4431 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4432 memcached_server_port(instance
), &rc
));
4434 test_true(rc
== MEMCACHED_SUCCESS
);
4436 return TEST_SUCCESS
;
4442 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4446 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4447 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4448 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4449 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4450 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4451 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4452 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4453 #ifdef HAVE_HSIEH_HASH
4454 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4456 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4457 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4458 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4460 return TEST_SUCCESS
;
4464 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4466 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4467 #ifdef HAVE_HSIEH_HASH
4468 expected_rc
= MEMCACHED_SUCCESS
;
4470 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4471 (uint64_t)MEMCACHED_HASH_HSIEH
);
4472 test_true(rc
== expected_rc
);
4474 return TEST_SUCCESS
;
4477 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4479 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4480 #ifdef HAVE_MURMUR_HASH
4481 expected_rc
= MEMCACHED_SUCCESS
;
4483 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4484 (uint64_t)MEMCACHED_HASH_MURMUR
);
4485 test_true(rc
== expected_rc
);
4487 return TEST_SUCCESS
;
4490 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4496 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4500 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4501 test_true(one_at_a_time_values
[x
] == hash_val
);
4504 return TEST_SUCCESS
;
4507 static test_return_t
md5_run (memcached_st
*memc
)
4513 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4517 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4518 test_true(md5_values
[x
] == hash_val
);
4521 return TEST_SUCCESS
;
4524 static test_return_t
crc_run (memcached_st
*memc
)
4530 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4534 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4535 test_true(crc_values
[x
] == hash_val
);
4538 return TEST_SUCCESS
;
4541 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4547 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4551 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4552 test_true(fnv1_64_values
[x
] == hash_val
);
4555 return TEST_SUCCESS
;
4558 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4564 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4568 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4569 test_true(fnv1a_64_values
[x
] == hash_val
);
4572 return TEST_SUCCESS
;
4575 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4581 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4585 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4586 test_true(fnv1_32_values
[x
] == hash_val
);
4589 return TEST_SUCCESS
;
4592 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4598 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4602 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4603 test_true(fnv1a_32_values
[x
] == hash_val
);
4606 return TEST_SUCCESS
;
4609 static test_return_t
hsieh_run (memcached_st
*memc
)
4615 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4619 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4620 test_true(hsieh_values
[x
] == hash_val
);
4623 return TEST_SUCCESS
;
4626 static test_return_t
murmur_run (memcached_st
*memc
)
4628 #ifdef WORDS_BIGENDIAN
4629 (void)murmur_values
;
4630 return TEST_SKIPPED
;
4636 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4640 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4641 test_true(murmur_values
[x
] == hash_val
);
4644 return TEST_SUCCESS
;
4648 static test_return_t
jenkins_run (memcached_st
*memc
)
4654 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4658 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4659 test_true(jenkins_values
[x
] == hash_val
);
4662 return TEST_SUCCESS
;
4665 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4668 return libhashkit_md5(string
, string_length
);
4671 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4674 return libhashkit_crc32(string
, string_length
);
4677 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4681 const hashkit_st
*kit
;
4683 hashkit_return_t hash_rc
;
4685 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};
4686 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};
4688 kit
= memcached_get_hashkit(memc
);
4690 hashkit_clone(&new_kit
, kit
);
4691 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4692 test_true(hash_rc
== HASHKIT_SUCCESS
);
4694 memcached_set_hashkit(memc
, &new_kit
);
4697 Verify Setting the hash.
4699 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4703 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4704 test_true(md5_values
[x
] == hash_val
);
4709 Now check memcached_st.
4711 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4715 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4716 test_true(md5_hosts
[x
] == hash_val
);
4719 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4720 test_true(hash_rc
== HASHKIT_SUCCESS
);
4722 memcached_set_hashkit(memc
, &new_kit
);
4725 Verify Setting the hash.
4727 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4731 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4732 test_true(crc_values
[x
] == hash_val
);
4735 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4739 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4740 test_true(crc_hosts
[x
] == hash_val
);
4743 return TEST_SUCCESS
;
4747 Test case adapted from John Gorman <johngorman2@gmail.com>
4749 We are testing the error condition when we connect to a server via memcached_get()
4750 but find that the server is not available.
4752 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4755 memcached_st
*tl_memc_h
;
4756 memcached_server_st
*servers
;
4758 const char *key
= "MemcachedLives";
4761 memcached_return rc
;
4765 tl_memc_h
= memcached_create(NULL
);
4766 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4767 memcached_server_push(tl_memc_h
, servers
);
4768 memcached_server_list_free(servers
);
4770 // See if memcached is reachable.
4771 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4774 test_true(len
== 0);
4775 test_false(rc
== MEMCACHED_SUCCESS
);
4777 memcached_free(tl_memc_h
);
4779 return TEST_SUCCESS
;
4783 We connect to a server which exists, but search for a key that does not exist.
4785 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4787 const char *key
= "MemcachedKeyNotEXIST";
4790 memcached_return rc
;
4793 // See if memcached is reachable.
4794 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4797 test_true(len
== 0);
4798 test_true(rc
== MEMCACHED_NOTFOUND
);
4800 return TEST_SUCCESS
;
4804 Test case adapted from John Gorman <johngorman2@gmail.com>
4806 We are testing the error condition when we connect to a server via memcached_get_by_key()
4807 but find that the server is not available.
4809 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4812 memcached_st
*tl_memc_h
;
4813 memcached_server_st
*servers
;
4815 const char *key
= "MemcachedLives";
4818 memcached_return rc
;
4822 tl_memc_h
= memcached_create(NULL
);
4823 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4824 memcached_server_push(tl_memc_h
, servers
);
4825 memcached_server_list_free(servers
);
4827 // See if memcached is reachable.
4828 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4831 test_true(len
== 0);
4832 test_false(rc
== MEMCACHED_SUCCESS
);
4834 memcached_free(tl_memc_h
);
4836 return TEST_SUCCESS
;
4840 We connect to a server which exists, but search for a key that does not exist.
4842 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4844 const char *key
= "MemcachedKeyNotEXIST";
4847 memcached_return rc
;
4850 // See if memcached is reachable.
4851 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4854 test_true(len
== 0);
4855 test_true(rc
== MEMCACHED_NOTFOUND
);
4857 return TEST_SUCCESS
;
4861 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4863 memcached_return_t rc
;
4866 memcached_server_st
*server_pool
;
4871 memc
= memcached_create(NULL
);
4874 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4875 test_true(rc
== MEMCACHED_SUCCESS
);
4877 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4878 test_true(value
== 1);
4880 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4881 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4884 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");
4885 memcached_server_push(memc
, server_pool
);
4887 /* verify that the server list was parsed okay. */
4888 test_true(memcached_server_count(memc
) == 8);
4889 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4890 test_true(server_pool
[0].port
== 11211);
4891 test_true(server_pool
[0].weight
== 600);
4892 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4893 test_true(server_pool
[2].port
== 11211);
4894 test_true(server_pool
[2].weight
== 200);
4895 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4896 test_true(server_pool
[7].port
== 11211);
4897 test_true(server_pool
[7].weight
== 100);
4899 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4900 * us test the boundary wraparound.
4902 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4904 /* verify the standard ketama set. */
4905 for (x
= 0; x
< 99; x
++)
4907 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4908 memcached_server_instance_st instance
=
4909 memcached_server_instance_by_position(memc
, server_idx
);
4910 const char *hostname
= memcached_server_name(instance
);
4912 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4915 memcached_server_list_free(server_pool
);
4916 memcached_free(memc
);
4918 return TEST_SUCCESS
;
4921 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4923 memcached_return_t rc
;
4925 memcached_server_st
*server_pool
;
4930 memc
= memcached_create(NULL
);
4933 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4934 test_true(rc
== MEMCACHED_SUCCESS
);
4936 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4937 test_true(value
== 1);
4939 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4940 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4942 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");
4943 assert(server_pool
);
4944 memcached_server_push(memc
, server_pool
);
4946 /* verify that the server list was parsed okay. */
4947 test_true(memcached_server_count(memc
) == 8);
4948 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4949 test_true(server_pool
[0].port
== 11211);
4950 test_true(server_pool
[0].weight
== 600);
4951 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4952 test_true(server_pool
[2].port
== 11211);
4953 test_true(server_pool
[2].weight
== 200);
4954 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4955 test_true(server_pool
[7].port
== 11211);
4956 test_true(server_pool
[7].weight
== 100);
4958 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4959 * us test the boundary wraparound.
4961 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4963 /* verify the standard ketama set. */
4964 for (uint32_t x
= 0; x
< 99; x
++)
4966 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4968 memcached_server_instance_st instance
=
4969 memcached_server_instance_by_position(memc
, server_idx
);
4971 const char *hostname
= memcached_server_name(instance
);
4973 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
4976 memcached_server_list_free(server_pool
);
4977 memcached_free(memc
);
4979 return TEST_SUCCESS
;
4982 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4984 test_return_t test_rc
;
4985 test_rc
= pre_binary(memc
);
4987 if (test_rc
!= TEST_SUCCESS
)
4990 const char *key
= "regression_bug_434484";
4991 size_t keylen
= strlen(key
);
4993 memcached_return_t ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4994 test_true(ret
== MEMCACHED_NOTSTORED
);
4996 size_t size
= 2048 * 1024;
4997 char *data
= (char*)calloc(1, size
);
4999 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5000 test_true(ret
== MEMCACHED_E2BIG
);
5003 return TEST_SUCCESS
;
5006 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5008 test_return_t test_rc
;
5009 test_rc
= pre_binary(memc
);
5011 if (test_rc
!= TEST_SUCCESS
)
5014 memcached_return_t rc
;
5016 memcached_execute_fn callbacks
[]= { &callback_counter
};
5019 * I only want to hit only _one_ server so I know the number of requests I'm
5020 * sending in the pipleine to the server. Let's try to do a multiget of
5021 * 1024 (that should satisfy most users don't you think?). Future versions
5022 * will include a mget_execute function call if you need a higher number.
5024 uint32_t number_of_hosts
= memcached_server_count(memc
);
5025 memc
->number_of_hosts
= 1;
5026 const size_t max_keys
= 1024;
5027 char **keys
= (char**)calloc(max_keys
, sizeof(char*));
5028 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5030 for (size_t x
= 0; x
< max_keys
; ++x
)
5034 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5036 test_true(keys
[x
] != NULL
);
5040 * Run two times.. the first time we should have 100% cache miss,
5041 * and the second time we should have 100% cache hits
5043 for (size_t y
= 0; y
< 2; y
++)
5045 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5046 test_true(rc
== MEMCACHED_SUCCESS
);
5047 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5051 /* The first iteration should give me a 100% cache miss. verify that*/
5052 char blob
[1024]= { 0 };
5054 test_true(counter
== 0);
5056 for (size_t x
= 0; x
< max_keys
; ++x
)
5058 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5059 blob
, sizeof(blob
), 0, 0);
5060 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5065 /* Verify that we received all of the key/value pairs */
5066 test_true(counter
== max_keys
);
5070 /* Release allocated resources */
5071 for (size_t x
= 0; x
< max_keys
; ++x
)
5078 memc
->number_of_hosts
= number_of_hosts
;
5080 return TEST_SUCCESS
;
5083 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5085 memcached_return_t rc
;
5086 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5087 test_true(rc
== MEMCACHED_SUCCESS
);
5089 return regression_bug_434843(memc
);
5092 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5094 memcached_return_t rc
;
5095 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5096 test_true(rc
== MEMCACHED_SUCCESS
);
5098 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5099 test_true(rc
== MEMCACHED_SUCCESS
);
5100 test_true(bytes
!= NULL
);
5101 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5103 test_true(rc
== MEMCACHED_SUCCESS
);
5104 test_true(bytes_read
!= NULL
);
5106 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5107 "bytes_written", &rc
);
5108 test_true(rc
== MEMCACHED_SUCCESS
);
5109 test_true(bytes_written
!= NULL
);
5111 test_true(strcmp(bytes
, bytes_read
) != 0);
5112 test_true(strcmp(bytes
, bytes_written
) != 0);
5114 /* Release allocated resources */
5117 free(bytes_written
);
5118 memcached_stat_free(NULL
, memc_stat
);
5120 return TEST_SUCCESS
;
5124 * The test case isn't obvious so I should probably document why
5125 * it works the way it does. Bug 442914 was caused by a bug
5126 * in the logic in memcached_purge (it did not handle the case
5127 * where the number of bytes sent was equal to the watermark).
5128 * In this test case, create messages so that we hit that case
5129 * and then disable noreply mode and issue a new command to
5130 * verify that it isn't stuck. If we change the format for the
5131 * delete command or the watermarks, we need to update this
5134 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5136 memcached_return_t rc
;
5137 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5138 test_true(rc
== MEMCACHED_SUCCESS
);
5139 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5141 uint32_t number_of_hosts
= memcached_server_count(memc
);
5142 memc
->number_of_hosts
= 1;
5147 for (uint32_t x
= 0; x
< 250; ++x
)
5149 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5150 rc
= memcached_delete(memc
, k
, len
, 0);
5151 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5154 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5157 rc
= memcached_delete(memc
, k
, len
, 0);
5158 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5160 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5161 test_true(rc
== MEMCACHED_SUCCESS
);
5162 rc
= memcached_delete(memc
, k
, len
, 0);
5163 test_true(rc
== MEMCACHED_NOTFOUND
);
5165 memc
->number_of_hosts
= number_of_hosts
;
5167 return TEST_SUCCESS
;
5170 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5172 memcached_server_instance_st instance_one
;
5173 memcached_server_instance_st instance_two
;
5175 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5176 return TEST_SKIPPED
;
5178 memcached_return_t rc
;
5180 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5181 test_true(rc
== MEMCACHED_SUCCESS
);
5183 const size_t max_keys
= 100;
5184 char **keys
= (char**)calloc(max_keys
, sizeof(char*));
5185 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5187 for (size_t x
= 0; x
< max_keys
; ++x
)
5191 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5193 test_true(keys
[x
] != NULL
);
5194 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5195 test_true(rc
== MEMCACHED_SUCCESS
);
5199 ** We are using the quiet commands to store the replicas, so we need
5200 ** to ensure that all of them are processed before we can continue.
5201 ** In the test we go directly from storing the object to trying to
5202 ** receive the object from all of the different servers, so we
5203 ** could end up in a race condition (the memcached server hasn't yet
5204 ** processed the quiet command from the replication set when it process
5205 ** the request from the other client (created by the clone)). As a
5206 ** workaround for that we call memcached_quit to send the quit command
5207 ** to the server and wait for the response ;-) If you use the test code
5208 ** as an example for your own code, please note that you shouldn't need
5211 memcached_quit(memc
);
5213 /* Verify that all messages are stored, and we didn't stuff too much
5216 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5217 test_true(rc
== MEMCACHED_SUCCESS
);
5220 memcached_execute_fn callbacks
[]= { &callback_counter
};
5221 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5222 /* Verify that we received all of the key/value pairs */
5223 test_true(counter
== max_keys
);
5225 memcached_quit(memc
);
5227 * Don't do the following in your code. I am abusing the internal details
5228 * within the library, and this is not a supported interface.
5229 * This is to verify correct behavior in the library. Fake that two servers
5232 instance_one
= memcached_server_instance_by_position(memc
, 0);
5233 instance_two
= memcached_server_instance_by_position(memc
, 2);
5234 in_port_t port0
= instance_one
->port
;
5235 in_port_t port2
= instance_two
->port
;
5237 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5238 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5240 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5241 test_true(rc
== MEMCACHED_SUCCESS
);
5244 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5245 test_true(counter
== (unsigned int)max_keys
);
5247 /* restore the memc handle */
5248 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5249 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5251 memcached_quit(memc
);
5253 /* Remove half of the objects */
5254 for (size_t x
= 0; x
< max_keys
; ++x
)
5258 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5259 test_true(rc
== MEMCACHED_SUCCESS
);
5263 memcached_quit(memc
);
5264 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5265 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5267 /* now retry the command, this time we should have cache misses */
5268 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5269 test_true(rc
== MEMCACHED_SUCCESS
);
5272 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5273 test_true(counter
== (unsigned int)(max_keys
>> 1));
5275 /* Release allocated resources */
5276 for (size_t x
= 0; x
< max_keys
; ++x
)
5283 /* restore the memc handle */
5284 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5285 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5287 return TEST_SUCCESS
;
5290 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5292 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5293 test_true(memc_clone
!= NULL
);
5294 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5296 memcached_server_instance_st instance
=
5297 memcached_server_instance_by_position(memc_clone
, 0);
5299 if (instance
->major_version
> 1 ||
5300 (instance
->major_version
== 1 &&
5301 instance
->minor_version
> 2))
5303 /* Binary protocol doesn't support deferred delete */
5304 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5305 test_true(bin_clone
!= NULL
);
5306 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5307 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5308 memcached_free(bin_clone
);
5310 memcached_quit(memc_clone
);
5312 /* If we know the server version, deferred delete should fail
5313 * with invalid arguments */
5314 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5316 /* If we don't know the server version, we should get a protocol error */
5317 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5319 /* but there is a bug in some of the memcached servers (1.4) that treats
5320 * the counter as noreply so it doesn't send the proper error message
5322 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5324 /* And buffered mode should be disabled and we should get protocol error */
5325 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5326 rc
= memcached_delete(memc
, "foo", 3, 1);
5327 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5329 /* Same goes for noreply... */
5330 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5331 rc
= memcached_delete(memc
, "foo", 3, 1);
5332 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5334 /* but a normal request should go through (and be buffered) */
5335 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5336 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5338 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5339 /* unbuffered noreply should be success */
5340 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5341 /* unbuffered with reply should be not found... */
5342 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5343 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5346 memcached_free(memc_clone
);
5347 return TEST_SUCCESS
;
5351 /* Test memcached_server_get_last_disconnect
5352 * For a working server set, shall be NULL
5353 * For a set of non existing server, shall not be NULL
5355 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5357 memcached_return_t rc
;
5358 memcached_server_instance_st disconnected_server
;
5360 /* With the working set of server */
5361 const char *key
= "marmotte";
5362 const char *value
= "milka";
5364 memcached_reset_last_disconnected_server(memc
);
5365 rc
= memcached_set(memc
, key
, strlen(key
),
5366 value
, strlen(value
),
5367 (time_t)0, (uint32_t)0);
5368 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5370 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5371 test_true(disconnected_server
== NULL
);
5373 /* With a non existing server */
5375 memcached_server_st
*servers
;
5377 const char *server_list
= "localhost:9";
5379 servers
= memcached_servers_parse(server_list
);
5381 mine
= memcached_create(NULL
);
5382 rc
= memcached_server_push(mine
, servers
);
5383 test_true(rc
== MEMCACHED_SUCCESS
);
5384 memcached_server_list_free(servers
);
5387 rc
= memcached_set(mine
, key
, strlen(key
),
5388 value
, strlen(value
),
5389 (time_t)0, (uint32_t)0);
5390 test_true(rc
!= MEMCACHED_SUCCESS
);
5392 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5393 if (disconnected_server
== NULL
)
5395 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5398 test_true(disconnected_server
!= NULL
);
5399 test_true(memcached_server_port(disconnected_server
)== 9);
5400 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5402 memcached_quit(mine
);
5403 memcached_free(mine
);
5405 return TEST_SUCCESS
;
5408 static test_return_t
test_verbosity(memcached_st
*memc
)
5410 memcached_verbosity(memc
, 3);
5412 return TEST_SUCCESS
;
5415 static test_return_t
test_server_failure(memcached_st
*memc
)
5417 memcached_st
*local_memc
;
5418 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5420 local_memc
= memcached_create(NULL
);
5422 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5423 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5425 uint32_t server_count
= memcached_server_count(local_memc
);
5427 test_true(server_count
== 1);
5429 // Disable the server
5430 instance
= memcached_server_instance_by_position(local_memc
, 0);
5431 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5433 memcached_return_t rc
;
5434 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5436 (time_t)0, (uint32_t)0);
5437 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5439 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5440 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5442 (time_t)0, (uint32_t)0);
5443 test_true(rc
== MEMCACHED_SUCCESS
);
5446 memcached_free(local_memc
);
5448 return TEST_SUCCESS
;
5451 static test_return_t
test_cull_servers(memcached_st
*memc
)
5453 uint32_t count
= memcached_server_count(memc
);
5455 // Do not do this in your code, it is not supported.
5456 memc
->servers
[1].options
.is_dead
= true;
5457 memc
->state
.is_time_for_rebuild
= true;
5459 uint32_t new_count
= memcached_server_count(memc
);
5460 test_true(count
== new_count
);
5463 test_true(count
== new_count
+ 1 );
5466 return TEST_SUCCESS
;
5470 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5471 const char *key
, size_t key_length
,
5472 const char *value
, size_t value_length
,
5482 return MEMCACHED_SUCCESS
;
5485 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5487 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5488 test_true(rc
== MEMCACHED_SUCCESS
);
5490 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5491 test_true(rc
== MEMCACHED_SUCCESS
);
5493 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5494 test_true(rc
== MEMCACHED_SUCCESS
);
5496 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5497 test_true(rc
== MEMCACHED_SUCCESS
);
5499 return TEST_SUCCESS
;
5503 * This test ensures that the failure counter isn't incremented during
5504 * normal termination of the memcached instance.
5506 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5508 memcached_return_t rc
;
5509 memcached_server_instance_st instance
;
5511 /* Set value to force connection to the server */
5512 const char *key
= "marmotte";
5513 const char *value
= "milka";
5516 * Please note that I'm abusing the internal structures in libmemcached
5517 * in a non-portable way and you shouldn't be doing this. I'm only
5518 * doing this in order to verify that the library works the way it should
5520 uint32_t number_of_hosts
= memcached_server_count(memc
);
5521 memc
->number_of_hosts
= 1;
5523 /* Ensure that we are connected to the server by setting a value */
5524 rc
= memcached_set(memc
, key
, strlen(key
),
5525 value
, strlen(value
),
5526 (time_t)0, (uint32_t)0);
5527 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5530 instance
= memcached_server_instance_by_position(memc
, 0);
5531 /* The test is to see that the memcached_quit doesn't increase the
5532 * the server failure conter, so let's ensure that it is zero
5533 * before sending quit
5535 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5537 memcached_quit(memc
);
5539 /* Verify that it memcached_quit didn't increment the failure counter
5540 * Please note that this isn't bullet proof, because an error could
5543 test_true(instance
->server_failure_counter
== 0);
5545 /* restore the instance */
5546 memc
->number_of_hosts
= number_of_hosts
;
5548 return TEST_SUCCESS
;
5552 * This tests ensures expected disconnections (for some behavior changes
5553 * for instance) do not wrongly increase failure counter
5555 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5557 memcached_return rc
;
5559 memcached_st
*memc_clone
;
5560 memc_clone
= memcached_clone(NULL
, memc
);
5561 test_true(memc_clone
);
5563 /* Set value to force connection to the server */
5564 const char *key
= "marmotte";
5565 const char *value
= "milka";
5566 char *string
= NULL
;
5567 size_t string_length
;
5570 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5571 value
, strlen(value
),
5572 (time_t)0, (uint32_t)0);
5573 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5576 /* put failure limit to 1 */
5577 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5578 assert(rc
== MEMCACHED_SUCCESS
);
5580 /* Put a retry timeout to effectively activate failure_limit effect */
5581 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5582 assert(rc
== MEMCACHED_SUCCESS
);
5584 /* change behavior that triggers memcached_quit()*/
5585 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5586 assert(rc
== MEMCACHED_SUCCESS
);
5589 /* Check if we still are connected */
5590 string
= memcached_get(memc_clone
, key
, strlen(key
),
5591 &string_length
, &flags
, &rc
);
5593 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5596 memcached_free(memc_clone
);
5598 return TEST_SUCCESS
;
5605 * Test that ensures mget_execute does not end into recursive calls that finally fails
5607 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5609 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5610 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5611 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5612 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5613 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5616 return TEST_SKIPPED
; // My MAC can't handle this test
5620 * I only want to hit _one_ server so I know the number of requests I'm
5621 * sending in the pipeline.
5623 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5624 memc
->number_of_hosts
= 1;
5625 size_t max_keys
= 20480;
5628 char **keys
= (char **)calloc(max_keys
, sizeof(char*));
5629 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5631 /* First add all of the items.. */
5633 char blob
[1024]= { 0 };
5634 memcached_return rc
;
5635 for (size_t x
= 0; x
< max_keys
; ++x
)
5638 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5640 assert(keys
[x
] != NULL
);
5641 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5643 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5645 break; // We are out of business
5648 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5650 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5653 sleep(1);// We will try to sleep
5656 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5658 // We failed to send everything.
5663 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5666 /* Try to get all of them with a large multiget */
5668 memcached_execute_function callbacks
[]= { &callback_counter
};
5669 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5670 (size_t)max_keys
, callbacks
, &counter
, 1);
5672 assert(rc
== MEMCACHED_SUCCESS
);
5673 char* the_value
= NULL
;
5674 char the_key
[MEMCACHED_MAX_KEY
];
5675 size_t the_key_length
;
5676 size_t the_value_length
;
5680 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5682 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5688 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5691 assert(rc
== MEMCACHED_END
);
5693 /* Verify that we got all of the items */
5694 assert(counter
== max_keys
);
5697 /* Release all allocated resources */
5698 for (size_t x
= 0; x
< max_keys
; ++x
)
5705 memc
->number_of_hosts
= number_of_hosts
;
5707 return TEST_SUCCESS
;
5710 static test_return_t
regression_bug_583031(memcached_st
*)
5712 memcached_st
*memc
= memcached_create(NULL
);
5714 test_true(memcached_success(memcached_server_add(memc
, "10.2.3.4", 11211)));
5716 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5717 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5718 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5719 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5720 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5721 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5723 memcached_return_t rc
;
5727 const char *value
= memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5729 test_compare(0, length
);
5731 test_true_got(rc
== MEMCACHED_TIMEOUT
|| rc
== MEMCACHED_ERRNO
|| rc
== MEMCACHED_FAILURE
, memcached_strerror(memc
, rc
));
5733 memcached_free(memc
);
5735 return TEST_SUCCESS
;
5738 static test_return_t
regression_bug_581030(memcached_st
*)
5741 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5742 test_false(local_stat
);
5744 memcached_stat_free(NULL
, NULL
);
5747 return TEST_SUCCESS
;
5750 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5752 fprintf(stderr
, "Iteration #%u: ", it
);
5754 if(error
== MEMCACHED_ERRNO
)
5756 fprintf(stderr
, "system error %d from %s: %s\n",
5757 errno
, what
, strerror(errno
));
5761 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5762 memcached_strerror(mc
, error
));
5766 #define TEST_CONSTANT_CREATION 200
5768 static test_return_t
regression_bug_(memcached_st
*memc
)
5770 const char *remote_server
;
5773 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5775 return TEST_SKIPPED
;
5778 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5780 memcached_st
* mc
= memcached_create(NULL
);
5781 memcached_return rc
;
5783 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5784 if (rc
!= MEMCACHED_SUCCESS
)
5786 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5789 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5790 if (rc
!= MEMCACHED_SUCCESS
)
5792 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5795 rc
= memcached_server_add(mc
, remote_server
, 0);
5796 if (rc
!= MEMCACHED_SUCCESS
)
5798 memcached_die(mc
, rc
, "memcached_server_add", x
);
5801 const char *set_key
= "akey";
5802 const size_t set_key_len
= strlen(set_key
);
5803 const char *set_value
= "a value";
5804 const size_t set_value_len
= strlen(set_value
);
5806 if (rc
== MEMCACHED_SUCCESS
)
5810 size_t get_value_len
;
5812 uint32_t get_value_flags
;
5814 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5815 &get_value_flags
, &rc
);
5816 if (rc
!= MEMCACHED_SUCCESS
)
5818 memcached_die(mc
, rc
, "memcached_get", x
);
5824 (get_value_len
!= set_value_len
5825 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5827 fprintf(stderr
, "Values don't match?\n");
5828 rc
= MEMCACHED_FAILURE
;
5834 rc
= memcached_set(mc
,
5835 set_key
, set_key_len
,
5836 set_value
, set_value_len
,
5840 if (rc
!= MEMCACHED_SUCCESS
)
5842 memcached_die(mc
, rc
, "memcached_set", x
);
5849 if (rc
!= MEMCACHED_SUCCESS
)
5855 return TEST_SUCCESS
;
5859 * Test that the sasl authentication works. We cannot use the default
5860 * pool of servers, because that would require that all servers we want
5861 * to test supports SASL authentication, and that they use the default
5864 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5866 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5867 memcached_return_t rc
;
5869 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5870 test_true(rc
== MEMCACHED_SUCCESS
);
5871 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5872 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5873 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5874 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5875 memcached_quit(memc
);
5877 rc
= memcached_set_sasl_auth_data(memc
,
5878 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5879 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5880 test_true(rc
== MEMCACHED_SUCCESS
);
5882 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5883 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5884 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5886 memcached_quit(memc
);
5887 return TEST_SUCCESS
;
5890 return TEST_FAILURE
;
5894 /* Clean the server before beginning testing */
5896 {"util_version", 1, (test_callback_fn
)util_version_test
},
5897 {"flush", 0, (test_callback_fn
)flush_test
},
5898 {"init", 0, (test_callback_fn
)init_test
},
5899 {"allocation", 0, (test_callback_fn
)allocation_test
},
5900 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5901 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5902 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5903 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5904 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5905 {"clone_test", 0, (test_callback_fn
)clone_test
},
5906 {"connection_test", 0, (test_callback_fn
)connection_test
},
5907 {"callback_test", 0, (test_callback_fn
)callback_test
},
5908 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5909 {"error", 0, (test_callback_fn
)error_test
},
5910 {"set", 0, (test_callback_fn
)set_test
},
5911 {"set2", 0, (test_callback_fn
)set_test2
},
5912 {"set3", 0, (test_callback_fn
)set_test3
},
5913 {"dump", 1, (test_callback_fn
)dump_test
},
5914 {"add", 1, (test_callback_fn
)add_test
},
5915 {"replace", 1, (test_callback_fn
)replace_test
},
5916 {"delete", 1, (test_callback_fn
)delete_test
},
5917 {"get", 1, (test_callback_fn
)get_test
},
5918 {"get2", 0, (test_callback_fn
)get_test2
},
5919 {"get3", 0, (test_callback_fn
)get_test3
},
5920 {"get4", 0, (test_callback_fn
)get_test4
},
5921 {"partial mget", 0, (test_callback_fn
)get_test5
},
5922 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5923 {"increment", 0, (test_callback_fn
)increment_test
},
5924 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5925 {"decrement", 0, (test_callback_fn
)decrement_test
},
5926 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5927 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5928 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5929 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5930 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5931 {"quit", 0, (test_callback_fn
)quit_test
},
5932 {"mget", 1, (test_callback_fn
)mget_test
},
5933 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5934 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5935 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5936 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5937 {"mget_end", 0, (test_callback_fn
)mget_end
},
5938 {"get_stats", 0, (test_callback_fn
)get_stats
},
5939 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5940 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5941 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5942 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5943 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5944 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5945 {"read_through", 1, (test_callback_fn
)read_through
},
5946 {"delete_through", 1, (test_callback_fn
)delete_through
},
5947 {"noreply", 1, (test_callback_fn
)noreply_test
},
5948 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5949 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5950 {"memcached_pool_test", 1, (test_callback_fn
)memcached_pool_test
},
5951 {"ping", 1, (test_callback_fn
)ping_test
},
5952 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5953 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
5954 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
5955 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
5956 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
5960 test_st behavior_tests
[] ={
5961 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5962 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5963 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
5964 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
5968 test_st basic_tests
[] ={
5969 {"init", 1, (test_callback_fn
)basic_init_test
},
5970 {"clone", 1, (test_callback_fn
)basic_clone_test
},
5971 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
5972 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
5973 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
5974 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
5978 test_st regression_binary_vs_block
[] ={
5979 {"block add", 1, (test_callback_fn
)block_add_regression
},
5980 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
5984 test_st async_tests
[] ={
5985 {"add", 1, (test_callback_fn
)add_wrapper
},
5989 test_st string_tests
[] ={
5990 {"string static with null", 0, (test_callback_fn
)string_static_null
},
5991 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
5992 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
5993 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
5994 {"string append", 0, (test_callback_fn
)string_alloc_append
},
5995 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
5996 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
5997 {0, 0, (test_callback_fn
)0}
6000 test_st result_tests
[] ={
6001 {"result static", 0, (test_callback_fn
)result_static
},
6002 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6003 {0, 0, (test_callback_fn
)0}
6006 test_st version_1_2_3
[] ={
6007 {"append", 0, (test_callback_fn
)append_test
},
6008 {"prepend", 0, (test_callback_fn
)prepend_test
},
6009 {"cas", 0, (test_callback_fn
)cas_test
},
6010 {"cas2", 0, (test_callback_fn
)cas2_test
},
6011 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6012 {0, 0, (test_callback_fn
)0}
6015 test_st user_tests
[] ={
6016 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6017 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6018 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6019 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6020 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6021 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6022 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6023 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6024 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6025 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6026 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6027 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6028 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6029 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6030 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6031 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6032 #if !defined(__sun) && !defined(__OpenBSD__)
6034 ** It seems to be something weird with the character sets..
6035 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6036 ** guess I need to find out how this is supposed to work.. Perhaps I need
6037 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6038 ** so just disable the code for now...).
6040 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6042 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6043 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6044 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6045 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6046 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6047 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6048 {0, 0, (test_callback_fn
)0}
6051 test_st replication_tests
[]= {
6052 {"set", 1, (test_callback_fn
)replication_set_test
},
6053 {"get", 0, (test_callback_fn
)replication_get_test
},
6054 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6055 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6056 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6057 {0, 0, (test_callback_fn
)0}
6061 * The following test suite is used to verify that we don't introduce
6062 * regression bugs. If you want more information about the bug / test,
6063 * you should look in the bug report at
6064 * http://bugs.launchpad.net/libmemcached
6066 test_st regression_tests
[]= {
6067 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6068 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6069 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6070 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6071 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6072 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6073 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6074 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6075 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6076 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6077 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6078 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6079 {0, 0, (test_callback_fn
)0}
6082 test_st sasl_auth_tests
[]= {
6083 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6084 {0, 0, (test_callback_fn
)0}
6087 test_st ketama_compatibility
[]= {
6088 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6089 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6090 {0, 0, (test_callback_fn
)0}
6093 test_st generate_tests
[] ={
6094 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6095 {"generate_data", 1, (test_callback_fn
)generate_data
},
6096 {"get_read", 0, (test_callback_fn
)get_read
},
6097 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6098 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6099 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6100 {"generate_data", 1, (test_callback_fn
)generate_data
},
6101 {"mget_read", 0, (test_callback_fn
)mget_read
},
6102 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6103 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6104 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6105 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6106 {"generate_data", 1, (test_callback_fn
)generate_data
},
6107 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6108 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6109 {0, 0, (test_callback_fn
)0}
6112 test_st consistent_tests
[] ={
6113 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6114 {"generate_data", 1, (test_callback_fn
)generate_data
},
6115 {"get_read", 0, (test_callback_fn
)get_read_count
},
6116 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6117 {0, 0, (test_callback_fn
)0}
6120 test_st consistent_weighted_tests
[] ={
6121 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6122 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6123 {"get_read", 0, (test_callback_fn
)get_read_count
},
6124 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6125 {0, 0, (test_callback_fn
)0}
6128 test_st hsieh_availability
[] ={
6129 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6130 {0, 0, (test_callback_fn
)0}
6133 test_st murmur_availability
[] ={
6134 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6135 {0, 0, (test_callback_fn
)0}
6139 test_st hash_sanity
[] ={
6140 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6141 {0, 0, (test_callback_fn
)0}
6145 test_st ketama_auto_eject_hosts
[] ={
6146 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6147 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6148 {0, 0, (test_callback_fn
)0}
6151 test_st hash_tests
[] ={
6152 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6153 {"md5", 0, (test_callback_fn
)md5_run
},
6154 {"crc", 0, (test_callback_fn
)crc_run
},
6155 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6156 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6157 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6158 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6159 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6160 {"murmur", 0, (test_callback_fn
)murmur_run
},
6161 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6162 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6163 {0, 0, (test_callback_fn
)0}
6166 test_st error_conditions
[] ={
6167 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6168 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6169 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6170 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6171 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6172 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6173 {0, 0, (test_callback_fn
)0}
6177 test_st parser_tests
[] ={
6178 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6179 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6180 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6181 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6182 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6183 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6184 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6185 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6186 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6187 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6188 {"server", 0, (test_callback_fn
)server_test
},
6189 {"bad server strings", 0, (test_callback_fn
)servers_bad_test
},
6190 {"server with weights", 0, (test_callback_fn
)server_with_weight_test
},
6191 {0, 0, (test_callback_fn
)0}
6194 test_st virtual_bucket_tests
[] ={
6195 {"basic", 0, (test_callback_fn
)virtual_back_map
},
6196 {0, 0, (test_callback_fn
)0}
6199 collection_st collection
[] ={
6201 {"hash_sanity", 0, 0, hash_sanity
},
6203 {"basic", 0, 0, basic_tests
},
6204 {"hsieh_availability", 0, 0, hsieh_availability
},
6205 {"murmur_availability", 0, 0, murmur_availability
},
6206 {"block", 0, 0, tests
},
6207 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6208 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6209 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6210 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6211 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6212 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6213 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6214 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6215 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6216 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6217 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6218 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6219 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6220 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6221 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6222 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6223 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6224 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6225 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6226 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6227 #ifdef MEMCACHED_ENABLE_DEPRECATED
6228 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6230 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6231 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6232 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6233 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6234 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6235 {"string", 0, 0, string_tests
},
6236 {"result", 0, 0, result_tests
},
6237 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6238 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6239 {"user", 0, 0, user_tests
},
6240 {"generate", 0, 0, generate_tests
},
6241 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6242 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6243 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6244 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6245 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6246 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6247 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6249 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6250 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6251 {"consistent_not", 0, 0, consistent_tests
},
6252 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6253 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6254 {"ketama_compat", 0, 0, ketama_compatibility
},
6255 {"test_hashes", 0, 0, hash_tests
},
6256 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6257 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6258 {"regression", 0, 0, regression_tests
},
6259 {"behaviors", 0, 0, behavior_tests
},
6260 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6261 {"error_conditions", 0, 0, error_conditions
},
6262 {"parser", 0, 0, parser_tests
},
6263 {"virtual buckets", 0, 0, virtual_bucket_tests
},
6267 #include "tests/libmemcached_world.h"
6269 void get_world(world_st
*world
)
6271 world
->collections
= collection
;
6273 world
->create
= (test_callback_create_fn
)world_create
;
6274 world
->destroy
= (test_callback_fn
)world_destroy
;
6276 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6277 world
->test
.flush
= (test_callback_fn
)world_flush
;
6278 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6279 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6280 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6282 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6283 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6285 world
->runner
= &defualt_libmemcached_runner
;