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.
51 #include <sys/types.h>
57 #include "libmemcached/common.h"
59 #include <libtest/server.h>
61 #include "clients/generator.h"
62 #include "clients/execute.h"
64 #define SMALL_STRING_LEN 1024
66 #include <libtest/test.h>
67 #include "tests/deprecated.h"
68 #include "tests/parser.h"
69 #include "tests/pool.h"
70 #include "tests/string.h"
71 #include "tests/replication.h"
72 #include "tests/basic.h"
73 #include "tests/error_conditions.h"
74 #include "tests/print.h"
75 #include "tests/virtual_buckets.h"
78 #ifdef HAVE_LIBMEMCACHEDUTIL
80 #include "libmemcached/memcached_util.h"
83 #include "hash_results.h"
85 #define GLOBAL_COUNT 10000
86 #define GLOBAL2_COUNT 100
87 #define SERVERS_TO_CREATE 5
88 static uint32_t global_count
;
90 static pairs_st
*global_pairs
;
91 static const char *global_keys
[GLOBAL_COUNT
];
92 static size_t global_keys_length
[GLOBAL_COUNT
];
95 static test_return_t
pre_binary(memcached_st
*memc
);
98 static test_return_t
init_test(memcached_st
*not_used
)
103 (void)memcached_create(&memc
);
104 memcached_free(&memc
);
109 #define TEST_PORT_COUNT 7
110 in_port_t test_ports
[TEST_PORT_COUNT
];
112 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
113 const memcached_server_st
*server
,
117 size_t bigger
= *((size_t *)(context
));
119 assert(bigger
<= memcached_server_port(server
));
120 *((size_t *)(context
))= memcached_server_port(server
);
122 return MEMCACHED_SUCCESS
;
125 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
126 const memcached_server_st
*instance
,
130 FILE *stream
= (FILE *)context
;
133 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
134 memcached_server_name(instance
),
135 memcached_server_port(instance
),
136 instance
->major_version
,
137 instance
->minor_version
,
138 instance
->micro_version
);
140 return MEMCACHED_SUCCESS
;
143 static test_return_t
server_sort_test(memcached_st
*ptr
)
145 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
147 memcached_return_t rc
;
148 memcached_server_fn callbacks
[1];
149 memcached_st
*local_memc
;
152 local_memc
= memcached_create(NULL
);
153 test_true(local_memc
);
154 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
156 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
158 test_ports
[x
]= (in_port_t
)random() % 64000;
159 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
160 test_true(memcached_server_count(local_memc
) == x
+ 1);
162 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
164 test_true(rc
== MEMCACHED_SUCCESS
);
167 callbacks
[0]= server_display_function
;
168 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
171 memcached_free(local_memc
);
176 static test_return_t
server_sort2_test(memcached_st
*ptr
)
178 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
179 memcached_return_t rc
;
180 memcached_server_fn callbacks
[1];
181 memcached_st
*local_memc
;
182 memcached_server_instance_st instance
;
185 local_memc
= memcached_create(NULL
);
186 test_true(local_memc
);
187 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
188 test_true(rc
== MEMCACHED_SUCCESS
);
190 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
191 test_true(rc
== MEMCACHED_SUCCESS
);
192 instance
= memcached_server_instance_by_position(local_memc
, 0);
193 test_true(memcached_server_port(instance
) == 43043);
195 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
196 test_true(rc
== MEMCACHED_SUCCESS
);
198 instance
= memcached_server_instance_by_position(local_memc
, 0);
199 test_true(memcached_server_port(instance
) == 43042);
201 instance
= memcached_server_instance_by_position(local_memc
, 1);
202 test_true(memcached_server_port(instance
) == 43043);
204 callbacks
[0]= server_display_function
;
205 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
208 memcached_free(local_memc
);
213 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
215 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";
218 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
221 memcached_server_fn callbacks
[1];
222 callbacks
[0]= server_print_callback
;
223 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
225 memcached_free(memc
);
230 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
231 const memcached_server_st
*server
,
235 uint32_t x
= *((uint32_t *)(context
));
238 if (! (test_ports
[x
] == server
->port
))
240 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)server
->port
);
241 return MEMCACHED_FAILURE
;
244 *((uint32_t *)(context
))= ++x
;
246 return MEMCACHED_SUCCESS
;
249 static test_return_t
server_unsort_test(memcached_st
*ptr
)
251 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
252 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
253 memcached_return_t rc
;
254 memcached_server_fn callbacks
[1];
255 memcached_st
*local_memc
;
258 local_memc
= memcached_create(NULL
);
259 test_true(local_memc
);
261 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
263 test_ports
[x
]= (in_port_t
)(random() % 64000);
264 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
265 test_true(memcached_server_count(local_memc
) == x
+1);
267 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
269 test_true(rc
== MEMCACHED_SUCCESS
);
272 callbacks
[0]= server_display_unsort_function
;
273 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
275 /* Now we sort old data! */
276 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
277 callbacks
[0]= server_display_function
;
278 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
281 memcached_free(local_memc
);
286 static test_return_t
allocation_test(memcached_st
*not_used
)
290 memc
= memcached_create(NULL
);
292 memcached_free(memc
);
297 static test_return_t
clone_test(memcached_st
*memc
)
301 memcached_st
*memc_clone
;
302 memc_clone
= memcached_clone(NULL
, NULL
);
303 test_true(memc_clone
);
304 memcached_free(memc_clone
);
307 /* Can we init from null? */
309 memcached_st
*memc_clone
;
310 memc_clone
= memcached_clone(NULL
, memc
);
311 test_true(memc_clone
);
314 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
315 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
316 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
317 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
320 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
321 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
322 test_true(memc_clone
->distribution
== memc
->distribution
);
323 { // Test all of the flags
324 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
325 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
326 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
327 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
328 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
329 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
330 test_true(memc_clone
->ketama
.weighted
== memc
->ketama
.weighted
);
331 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
332 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
333 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
334 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
335 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
336 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
338 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
339 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
340 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
341 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
342 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
343 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
344 test_true(memc_clone
->on_clone
== memc
->on_clone
);
345 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
346 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
347 test_true(memc_clone
->recv_size
== memc
->recv_size
);
348 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
349 test_true(memc_clone
->send_size
== memc
->send_size
);
350 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
351 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
352 test_true(memc_clone
->user_data
== memc
->user_data
);
354 memcached_free(memc_clone
);
357 /* Can we init from struct? */
359 memcached_st declared_clone
;
360 memcached_st
*memc_clone
;
361 memset(&declared_clone
, 0 , sizeof(memcached_st
));
362 memc_clone
= memcached_clone(&declared_clone
, NULL
);
363 test_true(memc_clone
);
364 memcached_free(memc_clone
);
367 /* Can we init from struct? */
369 memcached_st declared_clone
;
370 memcached_st
*memc_clone
;
371 memset(&declared_clone
, 0 , sizeof(memcached_st
));
372 memc_clone
= memcached_clone(&declared_clone
, memc
);
373 test_true(memc_clone
);
374 memcached_free(memc_clone
);
380 static test_return_t
userdata_test(memcached_st
*memc
)
383 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
384 test_true(memcached_get_user_data(memc
) == foo
);
385 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
390 static test_return_t
connection_test(memcached_st
*memc
)
392 memcached_return_t rc
;
394 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
395 test_true(rc
== MEMCACHED_SUCCESS
);
400 static test_return_t
error_test(memcached_st
*memc
)
402 memcached_return_t rc
;
403 uint32_t values
[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
404 982370485U, 1263635348U, 4242906218U, 3829656100U,
405 1891735253U, 334139633U, 2257084983U, 3088286104U,
406 13199785U, 2542027183U, 1097051614U, 199566778U,
407 2748246961U, 2465192557U, 1664094137U, 2405439045U,
408 1842224848U, 692413798U, 3479807801U, 919913813U,
409 4269430871U, 610793021U, 527273862U, 1437122909U,
410 2300930706U, 2943759320U, 674306647U, 2400528935U,
411 54481931U, 4186304426U, 1741088401U, 2979625118U,
412 4159057246U, 3425930182U, 2593724503U, 1868899624U,
413 1769812374U, 2302537950U, 1110330676U, 3365377466U,
414 1336171666U, 3021258493U, 3365377466U };
416 // You have updated the memcache_error messages but not updated docs/tests.
417 for (rc
= MEMCACHED_SUCCESS
; rc
< MEMCACHED_MAXIMUM_RETURN
; rc
++)
420 const char *msg
= memcached_strerror(memc
, rc
);
421 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
422 MEMCACHED_HASH_JENKINS
);
423 if (values
[rc
] != hash_val
)
425 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
426 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, rc
), hash_val
);
428 test_true(values
[rc
] == hash_val
);
430 test_true(MEMCACHED_MAXIMUM_RETURN
== 46);
435 static test_return_t
set_test(memcached_st
*memc
)
437 memcached_return_t rc
;
438 const char *key
= "foo";
439 const char *value
= "when we sanitize";
441 rc
= memcached_set(memc
, key
, strlen(key
),
442 value
, strlen(value
),
443 (time_t)0, (uint32_t)0);
444 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
449 static test_return_t
append_test(memcached_st
*memc
)
451 memcached_return_t rc
;
452 const char *key
= "fig";
453 const char *in_value
= "we";
454 char *out_value
= NULL
;
458 rc
= memcached_flush(memc
, 0);
459 test_true(rc
== MEMCACHED_SUCCESS
);
461 rc
= memcached_set(memc
, key
, strlen(key
),
462 in_value
, strlen(in_value
),
463 (time_t)0, (uint32_t)0);
464 test_true(rc
== MEMCACHED_SUCCESS
);
466 rc
= memcached_append(memc
, key
, strlen(key
),
467 " the", strlen(" the"),
468 (time_t)0, (uint32_t)0);
469 test_true(rc
== MEMCACHED_SUCCESS
);
471 rc
= memcached_append(memc
, key
, strlen(key
),
472 " people", strlen(" people"),
473 (time_t)0, (uint32_t)0);
474 test_true(rc
== MEMCACHED_SUCCESS
);
476 out_value
= memcached_get(memc
, key
, strlen(key
),
477 &value_length
, &flags
, &rc
);
478 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
479 test_true(strlen("we the people") == value_length
);
480 test_true(rc
== MEMCACHED_SUCCESS
);
486 static test_return_t
append_binary_test(memcached_st
*memc
)
488 memcached_return_t rc
;
489 const char *key
= "numbers";
490 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
496 rc
= memcached_flush(memc
, 0);
497 test_true(rc
== MEMCACHED_SUCCESS
);
499 rc
= memcached_set(memc
,
502 (time_t)0, (uint32_t)0);
503 test_true(rc
== MEMCACHED_SUCCESS
);
505 for (x
= 0; store_list
[x
] ; x
++)
507 rc
= memcached_append(memc
,
509 (char *)&store_list
[x
], sizeof(uint32_t),
510 (time_t)0, (uint32_t)0);
511 test_true(rc
== MEMCACHED_SUCCESS
);
514 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
515 &value_length
, &flags
, &rc
);
516 test_true((value_length
== (sizeof(uint32_t) * x
)));
517 test_true(rc
== MEMCACHED_SUCCESS
);
519 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
521 test_true(*ptr
== store_list
[x
- counter
]);
529 static test_return_t
cas2_test(memcached_st
*memc
)
531 memcached_return_t rc
;
532 const char *keys
[]= {"fudge", "son", "food"};
533 size_t key_length
[]= {5, 3, 4};
534 const char *value
= "we the people";
535 size_t value_length
= strlen("we the people");
537 memcached_result_st results_obj
;
538 memcached_result_st
*results
;
541 rc
= memcached_flush(memc
, 0);
542 test_true(rc
== MEMCACHED_SUCCESS
);
544 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
546 for (x
= 0; x
< 3; x
++)
548 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
549 keys
[x
], key_length
[x
],
550 (time_t)50, (uint32_t)9);
551 test_true(rc
== MEMCACHED_SUCCESS
);
554 rc
= memcached_mget(memc
, keys
, key_length
, 3);
556 results
= memcached_result_create(memc
, &results_obj
);
558 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
560 test_true(results
->item_cas
);
561 test_true(rc
== MEMCACHED_SUCCESS
);
562 test_true(memcached_result_cas(results
));
564 test_true(!memcmp(value
, "we the people", strlen("we the people")));
565 test_true(strlen("we the people") == value_length
);
566 test_true(rc
== MEMCACHED_SUCCESS
);
568 memcached_result_free(&results_obj
);
573 static test_return_t
cas_test(memcached_st
*memc
)
575 memcached_return_t rc
;
576 const char *key
= "fun";
577 size_t key_length
= strlen(key
);
578 const char *value
= "we the people";
579 const char* keys
[2] = { key
, NULL
};
580 size_t keylengths
[2] = { strlen(key
), 0 };
581 size_t value_length
= strlen(value
);
582 const char *value2
= "change the value";
583 size_t value2_length
= strlen(value2
);
585 memcached_result_st results_obj
;
586 memcached_result_st
*results
;
589 rc
= memcached_flush(memc
, 0);
590 test_true(rc
== MEMCACHED_SUCCESS
);
592 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
594 rc
= memcached_set(memc
, key
, strlen(key
),
595 value
, strlen(value
),
596 (time_t)0, (uint32_t)0);
597 test_true(rc
== MEMCACHED_SUCCESS
);
599 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
601 results
= memcached_result_create(memc
, &results_obj
);
603 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
605 test_true(rc
== MEMCACHED_SUCCESS
);
606 test_true(memcached_result_cas(results
));
607 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
608 test_true(strlen(memcached_result_value(results
)) == value_length
);
609 test_true(rc
== MEMCACHED_SUCCESS
);
610 uint64_t cas
= memcached_result_cas(results
);
613 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
614 test_true(rc
== MEMCACHED_END
);
615 test_true(results
== NULL
);
618 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
619 test_true(rc
== MEMCACHED_SUCCESS
);
622 * The item will have a new cas value, so try to set it again with the old
623 * value. This should fail!
625 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
626 test_true(rc
== MEMCACHED_DATA_EXISTS
);
628 memcached_result_free(&results_obj
);
633 static test_return_t
prepend_test(memcached_st
*memc
)
635 memcached_return_t rc
;
636 const char *key
= "fig";
637 const char *value
= "people";
638 char *out_value
= NULL
;
642 rc
= memcached_flush(memc
, 0);
643 test_true(rc
== MEMCACHED_SUCCESS
);
645 rc
= memcached_set(memc
, key
, strlen(key
),
646 value
, strlen(value
),
647 (time_t)0, (uint32_t)0);
648 test_true(rc
== MEMCACHED_SUCCESS
);
650 rc
= memcached_prepend(memc
, key
, strlen(key
),
651 "the ", strlen("the "),
652 (time_t)0, (uint32_t)0);
653 test_true(rc
== MEMCACHED_SUCCESS
);
655 rc
= memcached_prepend(memc
, key
, strlen(key
),
656 "we ", strlen("we "),
657 (time_t)0, (uint32_t)0);
658 test_true(rc
== MEMCACHED_SUCCESS
);
660 out_value
= memcached_get(memc
, key
, strlen(key
),
661 &value_length
, &flags
, &rc
);
662 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
663 test_true(strlen("we the people") == value_length
);
664 test_true(rc
== MEMCACHED_SUCCESS
);
671 Set the value, then quit to make sure it is flushed.
672 Come back in and test that add fails.
674 static test_return_t
add_test(memcached_st
*memc
)
676 memcached_return_t rc
;
677 const char *key
= "foo";
678 const char *value
= "when we sanitize";
679 unsigned long long setting_value
;
681 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
683 rc
= memcached_set(memc
, key
, strlen(key
),
684 value
, strlen(value
),
685 (time_t)0, (uint32_t)0);
686 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
687 memcached_quit(memc
);
688 rc
= memcached_add(memc
, key
, strlen(key
),
689 value
, strlen(value
),
690 (time_t)0, (uint32_t)0);
692 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
695 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
699 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
706 ** There was a problem of leaking filedescriptors in the initial release
707 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
708 ** systems it seems that the kernel is slow on reclaiming the resources
709 ** because the connects starts to time out (the test doesn't do much
710 ** anyway, so just loop 10 iterations)
712 static test_return_t
add_wrapper(memcached_st
*memc
)
714 unsigned int max
= 10000;
722 for (uint32_t x
= 0; x
< max
; x
++)
728 static test_return_t
replace_test(memcached_st
*memc
)
730 memcached_return_t rc
;
731 const char *key
= "foo";
732 const char *value
= "when we sanitize";
733 const char *original
= "first we insert some data";
735 rc
= memcached_set(memc
, key
, strlen(key
),
736 original
, strlen(original
),
737 (time_t)0, (uint32_t)0);
738 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
740 rc
= memcached_replace(memc
, key
, strlen(key
),
741 value
, strlen(value
),
742 (time_t)0, (uint32_t)0);
743 test_true(rc
== MEMCACHED_SUCCESS
);
748 static test_return_t
delete_test(memcached_st
*memc
)
750 memcached_return_t rc
;
751 const char *key
= "foo";
752 const char *value
= "when we sanitize";
754 rc
= memcached_set(memc
, key
, strlen(key
),
755 value
, strlen(value
),
756 (time_t)0, (uint32_t)0);
757 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
759 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
760 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
765 static test_return_t
flush_test(memcached_st
*memc
)
767 memcached_return_t rc
;
769 uint64_t query_id
= memcached_query_id(memc
);
770 rc
= memcached_flush(memc
, 0);
771 test_compare(rc
, MEMCACHED_SUCCESS
);
772 test_compare(query_id
+1, memcached_query_id(memc
));
777 static memcached_return_t
server_function(const memcached_st
*ptr
,
778 const memcached_server_st
*server
,
781 (void)ptr
; (void)server
; (void)context
;
784 return MEMCACHED_SUCCESS
;
787 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
790 strncpy(context
, "foo bad", sizeof(context
));
791 memcached_server_fn callbacks
[1];
793 callbacks
[0]= server_function
;
794 memcached_server_cursor(memc
, callbacks
, context
, 1);
798 static test_return_t
bad_key_test(memcached_st
*memc
)
800 memcached_return_t rc
;
801 const char *key
= "foo bad";
803 size_t string_length
;
805 memcached_st
*memc_clone
;
807 size_t max_keylen
= 0xffff;
809 // Just skip if we are in binary mode.
810 uint64_t query_id
= memcached_query_id(memc
);
811 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
813 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
815 memc_clone
= memcached_clone(NULL
, memc
);
816 test_true(memc_clone
);
818 query_id
= memcached_query_id(memc_clone
);
819 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
820 test_true(rc
== MEMCACHED_SUCCESS
);
821 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
823 /* All keys are valid in the binary protocol (except for length) */
824 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
826 query_id
= memcached_query_id(memc_clone
);
827 string
= memcached_get(memc_clone
, key
, strlen(key
),
828 &string_length
, &flags
, &rc
);
829 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
830 test_true(string_length
== 0);
834 query_id
= memcached_query_id(memc_clone
);
835 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
836 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
837 test_true(rc
== MEMCACHED_SUCCESS
);
838 string
= memcached_get(memc_clone
, key
, strlen(key
),
839 &string_length
, &flags
, &rc
);
840 test_true(rc
== MEMCACHED_NOTFOUND
);
841 test_true(string_length
== 0);
844 /* Test multi key for bad keys */
845 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
846 size_t key_lengths
[] = { 7, 7, 7 };
848 query_id
= memcached_query_id(memc_clone
);
849 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
850 test_true(rc
== MEMCACHED_SUCCESS
);
851 test_compare(query_id
, memcached_query_id(memc_clone
));
853 query_id
= memcached_query_id(memc_clone
);
854 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
855 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
856 test_compare(query_id
+1, memcached_query_id(memc_clone
));
858 query_id
= memcached_query_id(memc_clone
);
859 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
860 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
861 test_compare(query_id
+1, memcached_query_id(memc_clone
));
865 /* The following test should be moved to the end of this function when the
866 memcached server is updated to allow max size length of the keys in the
869 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
870 test_true(rc
== MEMCACHED_SUCCESS
);
872 char *longkey
= (char *)malloc(max_keylen
+ 1);
875 memset(longkey
, 'a', max_keylen
+ 1);
876 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
877 &string_length
, &flags
, &rc
);
878 test_true(rc
== MEMCACHED_NOTFOUND
);
879 test_true(string_length
== 0);
882 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
883 &string_length
, &flags
, &rc
);
884 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
885 test_true(string_length
== 0);
892 /* Make sure zero length keys are marked as bad */
894 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
895 test_true(rc
== MEMCACHED_SUCCESS
);
896 string
= memcached_get(memc_clone
, key
, 0,
897 &string_length
, &flags
, &rc
);
898 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
899 test_true(string_length
== 0);
902 memcached_free(memc_clone
);
907 #define READ_THROUGH_VALUE "set for me"
908 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
911 memcached_result_st
*result
)
913 (void)memc
;(void)key
;(void)key_length
;
914 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
917 static test_return_t
read_through(memcached_st
*memc
)
919 memcached_return_t rc
;
920 const char *key
= "foo";
922 size_t string_length
;
924 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
926 string
= memcached_get(memc
, key
, strlen(key
),
927 &string_length
, &flags
, &rc
);
929 test_true(rc
== MEMCACHED_NOTFOUND
);
930 test_false(string_length
);
933 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
,
935 test_true(rc
== MEMCACHED_SUCCESS
);
937 string
= memcached_get(memc
, key
, strlen(key
),
938 &string_length
, &flags
, &rc
);
940 test_true(rc
== MEMCACHED_SUCCESS
);
941 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
942 test_strcmp(READ_THROUGH_VALUE
, string
);
945 string
= memcached_get(memc
, key
, strlen(key
),
946 &string_length
, &flags
, &rc
);
948 test_true(rc
== MEMCACHED_SUCCESS
);
949 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
950 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
956 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
960 (void)ptr
;(void)key_length
;
963 return MEMCACHED_SUCCESS
;
966 static test_return_t
delete_through(memcached_st
*memc
)
968 memcached_trigger_delete_key_fn callback
;
969 memcached_return_t rc
;
971 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
973 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
974 test_true(rc
== MEMCACHED_SUCCESS
);
979 static test_return_t
get_test(memcached_st
*memc
)
981 memcached_return_t rc
;
982 const char *key
= "foo";
984 size_t string_length
;
987 uint64_t query_id
= memcached_query_id(memc
);
988 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
989 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
990 test_compare(query_id
+1, memcached_query_id(memc
));
992 string
= memcached_get(memc
, key
, strlen(key
),
993 &string_length
, &flags
, &rc
);
995 test_true(rc
== MEMCACHED_NOTFOUND
);
996 test_false(string_length
);
1002 static test_return_t
get_test2(memcached_st
*memc
)
1004 memcached_return_t rc
;
1005 const char *key
= "foo";
1006 const char *value
= "when we sanitize";
1008 size_t string_length
;
1011 uint64_t query_id
= memcached_query_id(memc
);
1012 rc
= memcached_set(memc
, key
, strlen(key
),
1013 value
, strlen(value
),
1014 (time_t)0, (uint32_t)0);
1015 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1016 test_compare(query_id
+1, memcached_query_id(memc
));
1018 query_id
= memcached_query_id(memc
);
1019 string
= memcached_get(memc
, key
, strlen(key
),
1020 &string_length
, &flags
, &rc
);
1021 test_compare(query_id
+1, memcached_query_id(memc
));
1024 test_true(rc
== MEMCACHED_SUCCESS
);
1025 test_true(string_length
== strlen(value
));
1026 test_memcmp(string
, value
, string_length
);
1030 return TEST_SUCCESS
;
1033 static test_return_t
set_test2(memcached_st
*memc
)
1035 memcached_return_t rc
;
1036 const char *key
= "foo";
1037 const char *value
= "train in the brain";
1038 size_t value_length
= strlen(value
);
1041 for (x
= 0; x
< 10; x
++)
1043 rc
= memcached_set(memc
, key
, strlen(key
),
1044 value
, value_length
,
1045 (time_t)0, (uint32_t)0);
1046 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1049 return TEST_SUCCESS
;
1052 static test_return_t
set_test3(memcached_st
*memc
)
1054 memcached_return_t rc
;
1056 size_t value_length
= 8191;
1058 value
= (char*)malloc(value_length
);
1061 for (uint32_t x
= 0; x
< value_length
; x
++)
1062 value
[x
] = (char) (x
% 127);
1064 /* The dump test relies on there being at least 32 items in memcached */
1065 for (uint32_t x
= 0; x
< 32; x
++)
1069 snprintf(key
, sizeof(key
), "foo%u", x
);
1071 uint64_t query_id
= memcached_query_id(memc
);
1072 rc
= memcached_set(memc
, key
, strlen(key
),
1073 value
, value_length
,
1074 (time_t)0, (uint32_t)0);
1075 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1076 test_compare(query_id
+1, memcached_query_id(memc
));
1081 return TEST_SUCCESS
;
1084 static test_return_t
get_test3(memcached_st
*memc
)
1086 memcached_return_t rc
;
1087 const char *key
= "foo";
1089 size_t value_length
= 8191;
1091 size_t string_length
;
1095 value
= (char*)malloc(value_length
);
1098 for (x
= 0; x
< value_length
; x
++)
1099 value
[x
] = (char) (x
% 127);
1101 rc
= memcached_set(memc
, key
, strlen(key
),
1102 value
, value_length
,
1103 (time_t)0, (uint32_t)0);
1104 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1106 string
= memcached_get(memc
, key
, strlen(key
),
1107 &string_length
, &flags
, &rc
);
1109 test_true(rc
== MEMCACHED_SUCCESS
);
1111 test_true(string_length
== value_length
);
1112 test_true(!memcmp(string
, value
, string_length
));
1117 return TEST_SUCCESS
;
1120 static test_return_t
get_test4(memcached_st
*memc
)
1122 memcached_return_t rc
;
1123 const char *key
= "foo";
1125 size_t value_length
= 8191;
1127 size_t string_length
;
1131 value
= (char*)malloc(value_length
);
1134 for (x
= 0; x
< value_length
; x
++)
1135 value
[x
] = (char) (x
% 127);
1137 rc
= memcached_set(memc
, key
, strlen(key
),
1138 value
, value_length
,
1139 (time_t)0, (uint32_t)0);
1140 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1142 for (x
= 0; x
< 10; x
++)
1144 string
= memcached_get(memc
, key
, strlen(key
),
1145 &string_length
, &flags
, &rc
);
1147 test_true(rc
== MEMCACHED_SUCCESS
);
1149 test_true(string_length
== value_length
);
1150 test_true(!memcmp(string
, value
, string_length
));
1156 return TEST_SUCCESS
;
1160 * This test verifies that memcached_read_one_response doesn't try to
1161 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1162 * responses before you execute a storage command.
1164 static test_return_t
get_test5(memcached_st
*memc
)
1167 ** Request the same key twice, to ensure that we hash to the same server
1168 ** (so that we have multiple response values queued up) ;-)
1170 const char *keys
[]= { "key", "key" };
1171 size_t lengths
[]= { 3, 3 };
1175 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1176 keys
[0], lengths
[0], 0, 0);
1177 test_true(rc
== MEMCACHED_SUCCESS
);
1178 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1180 memcached_result_st results_obj
;
1181 memcached_result_st
*results
;
1182 results
=memcached_result_create(memc
, &results_obj
);
1184 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1186 memcached_result_free(&results_obj
);
1188 /* Don't read out the second result, but issue a set instead.. */
1189 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1190 test_true(rc
== MEMCACHED_SUCCESS
);
1192 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1193 &rlen
, &flags
, &rc
);
1194 test_true(val
== NULL
);
1195 test_true(rc
== MEMCACHED_NOTFOUND
);
1196 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1197 test_true(val
!= NULL
);
1198 test_true(rc
== MEMCACHED_SUCCESS
);
1201 return TEST_SUCCESS
;
1204 static test_return_t
mget_end(memcached_st
*memc
)
1206 const char *keys
[]= { "foo", "foo2" };
1207 size_t lengths
[]= { 3, 4 };
1208 const char *values
[]= { "fjord", "41" };
1210 memcached_return_t rc
;
1213 for (int i
= 0; i
< 2; i
++)
1215 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1216 (time_t)0, (uint32_t)0);
1217 test_true(rc
== MEMCACHED_SUCCESS
);
1221 size_t string_length
;
1224 // retrieve both via mget
1225 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1226 test_true(rc
== MEMCACHED_SUCCESS
);
1228 char key
[MEMCACHED_MAX_KEY
];
1231 // this should get both
1232 for (int i
= 0; i
< 2; i
++)
1234 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1236 test_true(rc
== MEMCACHED_SUCCESS
);
1238 if (key_length
== 4)
1240 test_true(string_length
== strlen(values
[val
]));
1241 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1245 // this should indicate end
1246 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1247 test_true(rc
== MEMCACHED_END
);
1250 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1251 test_true(rc
== MEMCACHED_SUCCESS
);
1253 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1254 test_true(key_length
== lengths
[0]);
1255 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1256 test_true(string_length
== strlen(values
[0]));
1257 test_true(strncmp(values
[0], string
, string_length
) == 0);
1258 test_true(rc
== MEMCACHED_SUCCESS
);
1261 // this should indicate end
1262 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1263 test_true(rc
== MEMCACHED_END
);
1265 return TEST_SUCCESS
;
1268 /* Do not copy the style of this code, I just access hosts to testthis function */
1269 static test_return_t
stats_servername_test(memcached_st
*memc
)
1271 memcached_return_t rc
;
1272 memcached_stat_st memc_stat
;
1273 memcached_server_instance_st instance
=
1274 memcached_server_instance_by_position(memc
, 0);
1276 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1277 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1278 return TEST_SKIPPED
;
1280 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1281 memcached_server_name(instance
),
1282 memcached_server_port(instance
));
1284 return TEST_SUCCESS
;
1287 static test_return_t
increment_test(memcached_st
*memc
)
1289 uint64_t new_number
;
1290 memcached_return_t rc
;
1291 const char *key
= "number";
1292 const char *value
= "0";
1294 rc
= memcached_set(memc
, key
, strlen(key
),
1295 value
, strlen(value
),
1296 (time_t)0, (uint32_t)0);
1297 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1299 rc
= memcached_increment(memc
, key
, strlen(key
),
1301 test_true(rc
== MEMCACHED_SUCCESS
);
1302 test_true(new_number
== 1);
1304 rc
= memcached_increment(memc
, key
, strlen(key
),
1306 test_true(rc
== MEMCACHED_SUCCESS
);
1307 test_true(new_number
== 2);
1309 return TEST_SUCCESS
;
1312 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1314 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1316 uint64_t new_number
;
1317 memcached_return_t rc
;
1318 const char *key
= "number";
1319 uint64_t initial
= 0;
1321 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1322 1, initial
, 0, &new_number
);
1323 test_true(rc
== MEMCACHED_SUCCESS
);
1324 test_true(new_number
== initial
);
1326 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1327 1, initial
, 0, &new_number
);
1328 test_true(rc
== MEMCACHED_SUCCESS
);
1329 test_true(new_number
== (initial
+ 1));
1331 return TEST_SUCCESS
;
1334 static test_return_t
decrement_test(memcached_st
*memc
)
1336 uint64_t new_number
;
1337 memcached_return_t rc
;
1338 const char *key
= "number";
1339 const char *value
= "3";
1341 rc
= memcached_set(memc
, key
, strlen(key
),
1342 value
, strlen(value
),
1343 (time_t)0, (uint32_t)0);
1344 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1346 rc
= memcached_decrement(memc
, key
, strlen(key
),
1348 test_true(rc
== MEMCACHED_SUCCESS
);
1349 test_true(new_number
== 2);
1351 rc
= memcached_decrement(memc
, key
, strlen(key
),
1353 test_true(rc
== MEMCACHED_SUCCESS
);
1354 test_true(new_number
== 1);
1356 return TEST_SUCCESS
;
1359 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1361 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1363 uint64_t new_number
;
1364 memcached_return_t rc
;
1365 const char *key
= "number";
1366 uint64_t initial
= 3;
1368 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1369 1, initial
, 0, &new_number
);
1370 test_true(rc
== MEMCACHED_SUCCESS
);
1371 test_true(new_number
== initial
);
1373 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1374 1, initial
, 0, &new_number
);
1375 test_true(rc
== MEMCACHED_SUCCESS
);
1376 test_true(new_number
== (initial
- 1));
1378 return TEST_SUCCESS
;
1381 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1383 uint64_t new_number
;
1384 memcached_return_t rc
;
1385 const char *master_key
= "foo";
1386 const char *key
= "number";
1387 const char *value
= "0";
1389 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1391 value
, strlen(value
),
1392 (time_t)0, (uint32_t)0);
1393 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1395 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1397 test_true(rc
== MEMCACHED_SUCCESS
);
1398 test_true(new_number
== 1);
1400 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1402 test_true(rc
== MEMCACHED_SUCCESS
);
1403 test_true(new_number
== 2);
1405 return TEST_SUCCESS
;
1408 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1410 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1412 uint64_t new_number
;
1413 memcached_return_t rc
;
1414 const char *master_key
= "foo";
1415 const char *key
= "number";
1416 uint64_t initial
= 0;
1418 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1420 1, initial
, 0, &new_number
);
1421 test_true(rc
== MEMCACHED_SUCCESS
);
1422 test_true(new_number
== initial
);
1424 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1426 1, initial
, 0, &new_number
);
1427 test_true(rc
== MEMCACHED_SUCCESS
);
1428 test_true(new_number
== (initial
+ 1));
1430 return TEST_SUCCESS
;
1433 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1435 uint64_t new_number
;
1436 memcached_return_t rc
;
1437 const char *master_key
= "foo";
1438 const char *key
= "number";
1439 const char *value
= "3";
1441 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1443 value
, strlen(value
),
1444 (time_t)0, (uint32_t)0);
1445 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1447 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1450 test_true(rc
== MEMCACHED_SUCCESS
);
1451 test_true(new_number
== 2);
1453 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1456 test_true(rc
== MEMCACHED_SUCCESS
);
1457 test_true(new_number
== 1);
1459 return TEST_SUCCESS
;
1462 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1464 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1466 uint64_t new_number
;
1467 memcached_return_t rc
;
1468 const char *master_key
= "foo";
1469 const char *key
= "number";
1470 uint64_t initial
= 3;
1472 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1474 1, initial
, 0, &new_number
);
1475 test_true(rc
== MEMCACHED_SUCCESS
);
1476 test_true(new_number
== initial
);
1478 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1480 1, initial
, 0, &new_number
);
1481 test_true(rc
== MEMCACHED_SUCCESS
);
1482 test_true(new_number
== (initial
- 1));
1484 return TEST_SUCCESS
;
1487 static test_return_t
quit_test(memcached_st
*memc
)
1489 memcached_return_t rc
;
1490 const char *key
= "fudge";
1491 const char *value
= "sanford and sun";
1493 rc
= memcached_set(memc
, key
, strlen(key
),
1494 value
, strlen(value
),
1495 (time_t)10, (uint32_t)3);
1496 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1497 memcached_quit(memc
);
1499 rc
= memcached_set(memc
, key
, strlen(key
),
1500 value
, strlen(value
),
1501 (time_t)50, (uint32_t)9);
1502 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1504 return TEST_SUCCESS
;
1507 static test_return_t
mget_result_test(memcached_st
*memc
)
1509 memcached_return_t rc
;
1510 const char *keys
[]= {"fudge", "son", "food"};
1511 size_t key_length
[]= {5, 3, 4};
1514 memcached_result_st results_obj
;
1515 memcached_result_st
*results
;
1517 results
= memcached_result_create(memc
, &results_obj
);
1519 test_true(&results_obj
== results
);
1521 /* We need to empty the server before continueing test */
1522 rc
= memcached_flush(memc
, 0);
1523 test_true(rc
== MEMCACHED_SUCCESS
);
1525 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1526 test_true(rc
== MEMCACHED_SUCCESS
);
1528 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1533 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1534 test_true(!results
);
1535 test_true(rc
== MEMCACHED_END
);
1537 for (x
= 0; x
< 3; x
++)
1539 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1540 keys
[x
], key_length
[x
],
1541 (time_t)50, (uint32_t)9);
1542 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1545 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1546 test_true(rc
== MEMCACHED_SUCCESS
);
1548 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1551 test_true(&results_obj
== results
);
1552 test_true(rc
== MEMCACHED_SUCCESS
);
1553 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1554 test_true(!memcmp(memcached_result_key_value(results
),
1555 memcached_result_value(results
),
1556 memcached_result_length(results
)));
1559 memcached_result_free(&results_obj
);
1561 return TEST_SUCCESS
;
1564 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1566 memcached_return_t rc
;
1567 const char *keys
[]= {"fudge", "son", "food"};
1568 size_t key_length
[]= {5, 3, 4};
1571 memcached_result_st
*results
;
1573 /* We need to empty the server before continueing test */
1574 rc
= memcached_flush(memc
, 0);
1575 test_true(rc
== MEMCACHED_SUCCESS
);
1577 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1578 test_true(rc
== MEMCACHED_SUCCESS
);
1580 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1584 test_true(!results
);
1585 test_true(rc
== MEMCACHED_END
);
1587 for (x
= 0; x
< 3; x
++)
1589 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1590 keys
[x
], key_length
[x
],
1591 (time_t)50, (uint32_t)9);
1592 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1595 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1596 test_true(rc
== MEMCACHED_SUCCESS
);
1599 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1602 test_true(rc
== MEMCACHED_SUCCESS
);
1603 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1604 test_true(!memcmp(memcached_result_key_value(results
),
1605 memcached_result_value(results
),
1606 memcached_result_length(results
)));
1607 memcached_result_free(results
);
1611 return TEST_SUCCESS
;
1614 /* Count the results */
1615 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1616 memcached_result_st
*result
,
1619 (void)ptr
; (void)result
;
1620 size_t *counter
= (size_t *)context
;
1622 *counter
= *counter
+ 1;
1624 return MEMCACHED_SUCCESS
;
1627 static test_return_t
mget_result_function(memcached_st
*memc
)
1629 memcached_return_t rc
;
1630 const char *keys
[]= {"fudge", "son", "food"};
1631 size_t key_length
[]= {5, 3, 4};
1634 memcached_execute_fn callbacks
[1];
1636 /* We need to empty the server before continueing test */
1637 rc
= memcached_flush(memc
, 0);
1638 for (x
= 0; x
< 3; x
++)
1640 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1641 keys
[x
], key_length
[x
],
1642 (time_t)50, (uint32_t)9);
1643 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1646 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1647 test_true(rc
== MEMCACHED_SUCCESS
);
1649 callbacks
[0]= &callback_counter
;
1651 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1653 test_true(counter
== 3);
1655 return TEST_SUCCESS
;
1658 static test_return_t
mget_test(memcached_st
*memc
)
1660 memcached_return_t rc
;
1661 const char *keys
[]= {"fudge", "son", "food"};
1662 size_t key_length
[]= {5, 3, 4};
1666 char return_key
[MEMCACHED_MAX_KEY
];
1667 size_t return_key_length
;
1669 size_t return_value_length
;
1671 /* We need to empty the server before continueing test */
1672 rc
= memcached_flush(memc
, 0);
1673 test_true(rc
== MEMCACHED_SUCCESS
);
1675 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1676 test_true(rc
== MEMCACHED_SUCCESS
);
1678 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1679 &return_value_length
, &flags
, &rc
)) != NULL
)
1681 test_true(return_value
);
1683 test_true(!return_value
);
1684 test_true(return_value_length
== 0);
1685 test_true(rc
== MEMCACHED_END
);
1687 for (x
= 0; x
< 3; x
++)
1689 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1690 keys
[x
], key_length
[x
],
1691 (time_t)50, (uint32_t)9);
1692 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1695 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1696 test_true(rc
== MEMCACHED_SUCCESS
);
1699 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1700 &return_value_length
, &flags
, &rc
)))
1702 test_true(return_value
);
1703 test_true(rc
== MEMCACHED_SUCCESS
);
1704 test_true(return_key_length
== return_value_length
);
1705 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1710 return TEST_SUCCESS
;
1713 static test_return_t
mget_execute(memcached_st
*memc
)
1717 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1721 * I only want to hit _one_ server so I know the number of requests I'm
1722 * sending in the pipeline.
1724 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1725 memc
->number_of_hosts
= 1;
1727 size_t max_keys
= 20480;
1730 char **keys
= calloc(max_keys
, sizeof(char*));
1731 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
1733 /* First add all of the items.. */
1734 char blob
[1024] = {0};
1735 memcached_return_t rc
;
1737 for (size_t x
= 0; x
< max_keys
; ++x
)
1741 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1743 test_true(keys
[x
] != NULL
);
1744 uint64_t query_id
= memcached_query_id(memc
);
1745 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1746 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1747 test_compare(query_id
+1, memcached_query_id(memc
));
1750 /* Try to get all of them with a large multiget */
1752 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
1753 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1754 max_keys
, callbacks
, &counter
, 1);
1756 if (rc
== MEMCACHED_SUCCESS
)
1759 uint64_t query_id
= memcached_query_id(memc
);
1760 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1761 test_true(rc
== MEMCACHED_END
);
1762 test_compare(query_id
, memcached_query_id(memc
));
1764 /* Verify that we got all of the items */
1765 test_true(counter
== max_keys
);
1767 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1769 test_true(counter
== 0);
1773 test_fail("note: this test functions differently when in binary mode");
1776 /* Release all allocated resources */
1777 for (size_t x
= 0; x
< max_keys
; ++x
)
1784 memc
->number_of_hosts
= number_of_hosts
;
1785 return TEST_SUCCESS
;
1788 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1790 static test_return_t
key_setup(memcached_st
*memc
)
1794 if (pre_binary(memc
) != TEST_SUCCESS
)
1795 return TEST_SKIPPED
;
1797 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1799 return TEST_SUCCESS
;
1802 static test_return_t
key_teardown(memcached_st
*memc
)
1805 pairs_free(global_pairs
);
1807 return TEST_SUCCESS
;
1810 static test_return_t
block_add_regression(memcached_st
*memc
)
1812 /* First add all of the items.. */
1813 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1815 memcached_return_t rc
;
1816 char blob
[1024] = {0};
1818 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1819 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1822 return TEST_SUCCESS
;
1825 static test_return_t
binary_add_regression(memcached_st
*memc
)
1827 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1828 test_return_t rc
= block_add_regression(memc
);
1829 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1833 static test_return_t
get_stats_keys(memcached_st
*memc
)
1837 memcached_stat_st memc_stat
;
1838 memcached_return_t rc
;
1840 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1841 test_true(rc
== MEMCACHED_SUCCESS
);
1842 for (ptr
= stat_list
; *ptr
; ptr
++)
1847 return TEST_SUCCESS
;
1850 static test_return_t
version_string_test(memcached_st
*memc
)
1852 const char *version_string
;
1855 version_string
= memcached_lib_version();
1857 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1859 return TEST_SUCCESS
;
1862 static test_return_t
get_stats(memcached_st
*memc
)
1866 memcached_return_t rc
;
1867 memcached_stat_st
*memc_stat
;
1869 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1870 test_true(rc
== MEMCACHED_SUCCESS
);
1872 test_true(rc
== MEMCACHED_SUCCESS
);
1873 test_true(memc_stat
);
1875 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1877 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1878 test_true(rc
== MEMCACHED_SUCCESS
);
1879 for (ptr
= stat_list
; *ptr
; ptr
++);
1884 memcached_stat_free(NULL
, memc_stat
);
1886 return TEST_SUCCESS
;
1889 static test_return_t
add_host_test(memcached_st
*memc
)
1892 memcached_server_st
*servers
;
1893 memcached_return_t rc
;
1894 char servername
[]= "0.example.com";
1896 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1898 test_true(1 == memcached_server_list_count(servers
));
1900 for (x
= 2; x
< 20; x
++)
1902 char buffer
[SMALL_STRING_LEN
];
1904 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1905 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1907 test_true(rc
== MEMCACHED_SUCCESS
);
1908 test_true(x
== memcached_server_list_count(servers
));
1911 rc
= memcached_server_push(memc
, servers
);
1912 test_true(rc
== MEMCACHED_SUCCESS
);
1913 rc
= memcached_server_push(memc
, servers
);
1914 test_true(rc
== MEMCACHED_SUCCESS
);
1916 memcached_server_list_free(servers
);
1918 return TEST_SUCCESS
;
1921 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1923 (void)parent
;(void)memc_clone
;
1924 return MEMCACHED_SUCCESS
;
1927 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1930 return MEMCACHED_SUCCESS
;
1933 static test_return_t
callback_test(memcached_st
*memc
)
1935 /* Test User Data */
1939 memcached_return_t rc
;
1941 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1942 test_true(rc
== MEMCACHED_SUCCESS
);
1943 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1944 test_true(*test_ptr
== x
);
1947 /* Test Clone Callback */
1949 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1950 void *clone_cb_ptr
= *(void **)&clone_cb
;
1951 void *temp_function
= NULL
;
1952 memcached_return_t rc
;
1954 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1956 test_true(rc
== MEMCACHED_SUCCESS
);
1957 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1958 test_true(temp_function
== clone_cb_ptr
);
1961 /* Test Cleanup Callback */
1963 memcached_cleanup_fn cleanup_cb
=
1964 (memcached_cleanup_fn
)cleanup_test_callback
;
1965 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1966 void *temp_function
= NULL
;
1967 memcached_return_t rc
;
1969 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1971 test_true(rc
== MEMCACHED_SUCCESS
);
1972 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1973 test_true(temp_function
== cleanup_cb_ptr
);
1976 return TEST_SUCCESS
;
1979 /* We don't test the behavior itself, we test the switches */
1980 static test_return_t
behavior_test(memcached_st
*memc
)
1985 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1986 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1987 test_true(value
== 1);
1989 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1990 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1991 test_true(value
== 1);
1993 set
= MEMCACHED_HASH_MD5
;
1994 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1995 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1996 test_true(value
== MEMCACHED_HASH_MD5
);
2000 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2001 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
2002 test_true(value
== 0);
2004 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2005 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
2006 test_true(value
== 0);
2008 set
= MEMCACHED_HASH_DEFAULT
;
2009 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2010 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2011 test_true(value
== MEMCACHED_HASH_DEFAULT
);
2013 set
= MEMCACHED_HASH_CRC
;
2014 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2015 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2016 test_true(value
== MEMCACHED_HASH_CRC
);
2018 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2019 test_true(value
> 0);
2021 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2022 test_true(value
> 0);
2024 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2025 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
2026 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2028 return TEST_SUCCESS
;
2031 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2033 memcached_return_t rc
;
2036 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2037 test_true(rc
== MEMCACHED_DEPRECATED
);
2039 // Platform dependent
2041 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2045 return TEST_SUCCESS
;
2049 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2051 memcached_return_t rc
;
2055 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2056 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2058 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2060 if (rc
== MEMCACHED_SUCCESS
)
2062 test_true((bool)value
== set
);
2066 test_false((bool)value
== set
);
2069 return TEST_SUCCESS
;
2073 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2075 memcached_return_t rc
;
2079 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2080 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2082 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2084 if (rc
== MEMCACHED_SUCCESS
)
2086 test_true((bool)value
== set
);
2090 test_false((bool)value
== set
);
2093 return TEST_SUCCESS
;
2096 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2098 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2099 char return_key
[MEMCACHED_MAX_KEY
];
2100 size_t return_key_length
;
2102 size_t return_value_length
;
2107 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2108 &return_value_length
, &flags
, &rc
)))
2110 test_true(return_value
);
2111 test_true(rc
== MEMCACHED_SUCCESS
);
2113 *keys_returned
= *keys_returned
+1;
2116 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2118 return TEST_SUCCESS
;
2121 /* Test case provided by Cal Haldenbrand */
2122 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2124 unsigned int setter
= 1;
2126 unsigned long long total
= 0;
2129 char randomstuff
[6 * 1024];
2130 memcached_return_t rc
;
2132 memset(randomstuff
, 0, 6 * 1024);
2134 /* We just keep looking at the same values over and over */
2137 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2138 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2142 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2146 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2147 memset(randomstuff
, 0, 6 * 1024);
2148 test_true(size
< 6 * 1024); /* Being safe here */
2150 for (j
= 0 ; j
< size
;j
++)
2151 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2154 snprintf(key
, sizeof(key
), "%u", x
);
2155 rc
= memcached_set(memc
, key
, strlen(key
),
2156 randomstuff
, strlen(randomstuff
), 10, 0);
2157 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2158 /* If we fail, lets try again */
2159 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2160 rc
= memcached_set(memc
, key
, strlen(key
),
2161 randomstuff
, strlen(randomstuff
), 10, 0);
2162 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2165 return TEST_SUCCESS
;
2168 /* Test case provided by Cal Haldenbrand */
2169 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2171 unsigned int setter
;
2175 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2178 setter
= 20 * 1024576;
2179 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2180 setter
= 20 * 1024576;
2181 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2182 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2183 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2185 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2188 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2190 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2191 char buffer
[SMALL_STRING_LEN
];
2196 memset(buffer
, 0, SMALL_STRING_LEN
);
2198 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2199 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2200 &val_len
, &flags
, &rc
);
2201 if (rc
!= MEMCACHED_SUCCESS
)
2203 if (rc
== MEMCACHED_NOTFOUND
)
2217 return TEST_SUCCESS
;
2220 /* Do a large mget() over all the keys we think exist */
2221 #define KEY_COUNT 3000 // * 1024576
2222 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2224 memcached_return_t rc
;
2225 unsigned int setter
;
2228 size_t key_lengths
[KEY_COUNT
];
2231 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2232 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2234 setter
= 20 * 1024576;
2235 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2236 setter
= 20 * 1024576;
2237 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2238 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2239 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2242 keys
= calloc(KEY_COUNT
, sizeof(char *));
2244 for (x
= 0; x
< KEY_COUNT
; x
++)
2248 snprintf(buffer
, 30, "%u", x
);
2249 keys
[x
]= strdup(buffer
);
2250 key_lengths
[x
]= strlen(keys
[x
]);
2253 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2254 test_true(rc
== MEMCACHED_SUCCESS
);
2256 size_t keys_returned
;
2257 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2259 for (x
= 0; x
< KEY_COUNT
; x
++)
2263 return TEST_SUCCESS
;
2266 /* Make sure we behave properly if server list has no values */
2267 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2269 memcached_return_t rc
;
2270 const char *keys
[]= {"fudge", "son", "food"};
2271 size_t key_length
[]= {5, 3, 4};
2274 char return_key
[MEMCACHED_MAX_KEY
];
2275 size_t return_key_length
;
2277 size_t return_value_length
;
2279 /* Here we free everything before running a bunch of mget tests */
2280 memcached_servers_reset(memc
);
2283 /* We need to empty the server before continueing test */
2284 rc
= memcached_flush(memc
, 0);
2285 test_compare(rc
, MEMCACHED_NO_SERVERS
);
2287 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2288 test_true(rc
== MEMCACHED_NO_SERVERS
);
2290 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2291 &return_value_length
, &flags
, &rc
)) != NULL
)
2293 test_true(return_value
);
2295 test_false(return_value
);
2296 test_true(return_value_length
== 0);
2297 test_true(rc
== MEMCACHED_NO_SERVERS
);
2299 for (x
= 0; x
< 3; x
++)
2301 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2302 keys
[x
], key_length
[x
],
2303 (time_t)50, (uint32_t)9);
2304 test_true(rc
== MEMCACHED_NO_SERVERS
);
2307 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2308 test_true(rc
== MEMCACHED_NO_SERVERS
);
2311 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2312 &return_value_length
, &flags
, &rc
)))
2314 test_true(return_value
);
2315 test_true(rc
== MEMCACHED_SUCCESS
);
2316 test_true(return_key_length
== return_value_length
);
2317 test_memcmp(return_value
, return_key
, return_value_length
);
2322 return TEST_SUCCESS
;
2325 #define VALUE_SIZE_BUG5 1048064
2326 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2328 memcached_return_t rc
;
2329 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2330 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2331 char return_key
[MEMCACHED_MAX_KEY
];
2332 size_t return_key_length
;
2334 size_t value_length
;
2338 char insert_data
[VALUE_SIZE_BUG5
];
2340 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2341 insert_data
[x
]= (signed char)rand();
2343 memcached_flush(memc
, 0);
2344 value
= memcached_get(memc
, keys
[0], key_length
[0],
2345 &value_length
, &flags
, &rc
);
2346 test_true(value
== NULL
);
2347 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2350 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2351 &value_length
, &flags
, &rc
)))
2353 test_true(count
== 0);
2355 for (x
= 0; x
< 4; x
++)
2357 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2358 insert_data
, VALUE_SIZE_BUG5
,
2359 (time_t)0, (uint32_t)0);
2360 test_true(rc
== MEMCACHED_SUCCESS
);
2363 for (x
= 0; x
< 10; x
++)
2365 value
= memcached_get(memc
, keys
[0], key_length
[0],
2366 &value_length
, &flags
, &rc
);
2370 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2372 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2373 &value_length
, &flags
, &rc
)))
2378 test_true(count
== 4);
2381 return TEST_SUCCESS
;
2384 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2386 memcached_return_t rc
;
2387 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2388 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2389 char return_key
[MEMCACHED_MAX_KEY
];
2390 size_t return_key_length
;
2392 size_t value_length
;
2396 char insert_data
[VALUE_SIZE_BUG5
];
2398 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2399 insert_data
[x
]= (signed char)rand();
2401 memcached_flush(memc
, 0);
2402 value
= memcached_get(memc
, keys
[0], key_length
[0],
2403 &value_length
, &flags
, &rc
);
2404 test_true(value
== NULL
);
2405 test_true(rc
== MEMCACHED_NOTFOUND
);
2406 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2407 test_true(rc
== MEMCACHED_SUCCESS
);
2410 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2411 &value_length
, &flags
, &rc
)))
2413 test_true(count
== 0);
2414 test_true(rc
== MEMCACHED_END
);
2416 for (x
= 0; x
< 4; x
++)
2418 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2419 insert_data
, VALUE_SIZE_BUG5
,
2420 (time_t)0, (uint32_t)0);
2421 test_true(rc
== MEMCACHED_SUCCESS
);
2424 for (x
= 0; x
< 2; x
++)
2426 value
= memcached_get(memc
, keys
[0], key_length
[0],
2427 &value_length
, &flags
, &rc
);
2431 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2432 test_true(rc
== MEMCACHED_SUCCESS
);
2434 /* We test for purge of partial complete fetches */
2435 for (count
= 3; count
; count
--)
2437 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2438 &value_length
, &flags
, &rc
);
2439 test_true(rc
== MEMCACHED_SUCCESS
);
2440 test_true(!(memcmp(value
, insert_data
, value_length
)));
2441 test_true(value_length
);
2446 return TEST_SUCCESS
;
2449 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2451 memcached_return_t rc
;
2453 memcached_st
*memc_clone
;
2455 memcached_server_st
*servers
;
2456 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";
2459 servers
= memcached_servers_parse(server_list
);
2462 mine
= memcached_create(NULL
);
2463 rc
= memcached_server_push(mine
, servers
);
2464 test_true(rc
== MEMCACHED_SUCCESS
);
2465 memcached_server_list_free(servers
);
2468 memc_clone
= memcached_clone(NULL
, mine
);
2470 memcached_quit(mine
);
2471 memcached_quit(memc_clone
);
2474 memcached_free(mine
);
2475 memcached_free(memc_clone
);
2477 return TEST_SUCCESS
;
2480 /* Test flag store/retrieve */
2481 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2483 memcached_return_t rc
;
2484 const char *keys
= "036790384900";
2485 size_t key_length
= strlen(keys
);
2486 char return_key
[MEMCACHED_MAX_KEY
];
2487 size_t return_key_length
;
2489 size_t value_length
;
2492 char insert_data
[VALUE_SIZE_BUG5
];
2494 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2495 insert_data
[x
]= (signed char)rand();
2497 memcached_flush(memc
, 0);
2500 rc
= memcached_set(memc
, keys
, key_length
,
2501 insert_data
, VALUE_SIZE_BUG5
,
2503 test_true(rc
== MEMCACHED_SUCCESS
);
2506 value
= memcached_get(memc
, keys
, key_length
,
2507 &value_length
, &flags
, &rc
);
2508 test_true(flags
== 245);
2512 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2515 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2516 &value_length
, &flags
, &rc
);
2517 test_true(flags
== 245);
2522 return TEST_SUCCESS
;
2525 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2527 memcached_return_t rc
;
2528 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2529 size_t key_length
[3];
2534 char return_key
[MEMCACHED_MAX_KEY
];
2535 size_t return_key_length
;
2537 size_t return_value_length
;
2540 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2541 key_length
[1]= strlen("fudge&*@#");
2542 key_length
[2]= strlen("for^#@&$not");
2545 for (x
= 0; x
< 3; x
++)
2547 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2548 keys
[x
], key_length
[x
],
2549 (time_t)50, (uint32_t)9);
2550 test_true(rc
== MEMCACHED_SUCCESS
);
2553 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2554 test_true(rc
== MEMCACHED_SUCCESS
);
2556 /* We need to empty the server before continueing test */
2557 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2558 &return_value_length
, &flags
, &rc
)) != NULL
)
2560 test_true(return_value
);
2564 test_true(count
== 3);
2566 return TEST_SUCCESS
;
2569 /* We are testing with aggressive timeout to get failures */
2570 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2572 const char *key
= "foo";
2574 size_t value_length
= 512;
2577 memcached_return_t rc
;
2578 unsigned int set
= 1;
2579 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2582 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2583 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2585 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2588 value
= (char*)malloc(value_length
* sizeof(char));
2590 for (x
= 0; x
< value_length
; x
++)
2591 value
[x
]= (char) (x
% 127);
2593 for (x
= 1; x
<= 100000; ++x
)
2595 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2597 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2598 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2600 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2605 memcached_free(mclone
);
2607 return TEST_SUCCESS
;
2611 We are looking failures in the async protocol
2613 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2615 const char *key
= "foo";
2617 size_t value_length
= 512;
2620 memcached_return_t rc
;
2621 unsigned int set
= 1;
2623 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2625 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2626 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2628 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2631 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2633 test_true(timeout
== -1);
2635 value
= (char*)malloc(value_length
* sizeof(char));
2637 for (x
= 0; x
< value_length
; x
++)
2638 value
[x
]= (char) (x
% 127);
2640 for (x
= 1; x
<= 100000; ++x
)
2642 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2646 memcached_free(mclone
);
2648 return TEST_SUCCESS
;
2652 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2654 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2656 memcached_return_t rc
;
2658 size_t value_length
;
2660 uint64_t number_value
;
2662 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2663 &value_length
, &flags
, &rc
);
2664 test_true(value
== NULL
);
2665 test_true(rc
== MEMCACHED_NOTFOUND
);
2667 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2670 test_true(value
== NULL
);
2671 /* The binary protocol will set the key if it doesn't exist */
2672 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2674 test_true(rc
== MEMCACHED_SUCCESS
);
2678 test_true(rc
== MEMCACHED_NOTFOUND
);
2681 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2683 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2684 &value_length
, &flags
, &rc
);
2686 test_true(rc
== MEMCACHED_SUCCESS
);
2689 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2691 test_true(number_value
== 2);
2692 test_true(rc
== MEMCACHED_SUCCESS
);
2694 return TEST_SUCCESS
;
2698 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2699 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2701 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2703 char key
[] = "key34567890";
2705 memcached_return_t rc
;
2706 size_t overflowSize
;
2708 char commandFirst
[]= "set key34567890 0 0 ";
2709 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2710 size_t commandLength
;
2713 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2715 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2717 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2719 overflow
= malloc(testSize
);
2720 test_true(overflow
!= NULL
);
2722 memset(overflow
, 'x', testSize
);
2723 rc
= memcached_set(memc
, key
, strlen(key
),
2724 overflow
, testSize
, 0, 0);
2725 test_true(rc
== MEMCACHED_SUCCESS
);
2729 return TEST_SUCCESS
;
2734 Test values of many different sizes
2735 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2736 set key34567890 0 0 8169 \r\n
2737 is sent followed by buffer of size 8169, followed by 8169
2739 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2742 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2743 memcached_return_t rc
;
2744 const char *key
= "foo";
2746 size_t value_length
= 18000;
2748 size_t string_length
;
2751 size_t current_length
;
2753 value
= (char*)malloc(value_length
);
2756 for (x
= 0; x
< value_length
; x
++)
2757 value
[x
] = (char) (x
% 127);
2759 for (current_length
= 0; current_length
< value_length
; current_length
++)
2761 rc
= memcached_set(memc
, key
, strlen(key
),
2762 value
, current_length
,
2763 (time_t)0, (uint32_t)0);
2764 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2766 string
= memcached_get(memc
, key
, strlen(key
),
2767 &string_length
, &flags
, &rc
);
2769 test_true(rc
== MEMCACHED_SUCCESS
);
2770 test_true(string_length
== current_length
);
2771 test_true(!memcmp(string
, value
, string_length
));
2778 return TEST_SUCCESS
;
2782 Look for zero length value problems
2784 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2787 memcached_return_t rc
;
2788 const char *key
= "mykey";
2793 for (x
= 0; x
< 2; x
++)
2795 rc
= memcached_set(memc
, key
, strlen(key
),
2797 (time_t)0, (uint32_t)0);
2799 test_true(rc
== MEMCACHED_SUCCESS
);
2801 value
= memcached_get(memc
, key
, strlen(key
),
2802 &length
, &flags
, &rc
);
2804 test_true(rc
== MEMCACHED_SUCCESS
);
2805 test_true(value
== NULL
);
2806 test_true(length
== 0);
2807 test_true(flags
== 0);
2809 value
= memcached_get(memc
, key
, strlen(key
),
2810 &length
, &flags
, &rc
);
2812 test_true(rc
== MEMCACHED_SUCCESS
);
2813 test_true(value
== NULL
);
2814 test_true(length
== 0);
2815 test_true(flags
== 0);
2818 return TEST_SUCCESS
;
2821 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2822 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2824 memcached_return_t rc
;
2825 const char *key
= "mykey";
2830 rc
= memcached_set(memc
, key
, strlen(key
),
2832 (time_t)0, UINT32_MAX
);
2834 test_true(rc
== MEMCACHED_SUCCESS
);
2836 value
= memcached_get(memc
, key
, strlen(key
),
2837 &length
, &flags
, &rc
);
2839 test_true(rc
== MEMCACHED_SUCCESS
);
2840 test_true(value
== NULL
);
2841 test_true(length
== 0);
2842 test_true(flags
== UINT32_MAX
);
2844 return TEST_SUCCESS
;
2847 #if !defined(__sun) && !defined(__OpenBSD__)
2848 /* Check the validity of chinese key*/
2849 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2851 memcached_return_t rc
;
2852 const char *key
= "豆瓣";
2853 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2858 rc
= memcached_set(memc
, key
, strlen(key
),
2859 value
, strlen(value
),
2862 test_true(rc
== MEMCACHED_SUCCESS
);
2864 value2
= memcached_get(memc
, key
, strlen(key
),
2865 &length
, &flags
, &rc
);
2867 test_true(length
==strlen(value
));
2868 test_true(rc
== MEMCACHED_SUCCESS
);
2869 test_true(memcmp(value
, value2
, length
)==0);
2872 return TEST_SUCCESS
;
2880 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2883 const memcached_server_st
*server
;
2884 memcached_return_t res
;
2888 memc
= memcached_create(NULL
);
2889 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2890 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2892 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2894 memcached_free(memc
);
2896 return TEST_SUCCESS
;
2899 /* CAS test from Andei */
2900 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2902 memcached_return_t status
;
2903 memcached_result_st
*result
, result_obj
;
2904 const char *key
= "abc";
2905 size_t key_len
= strlen("abc");
2906 const char *value
= "foobar";
2907 size_t value_len
= strlen(value
);
2909 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2911 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2912 test_true(status
== MEMCACHED_SUCCESS
);
2914 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2915 test_true(status
== MEMCACHED_SUCCESS
);
2917 result
= memcached_result_create(memc
, &result_obj
);
2920 memcached_result_create(memc
, &result_obj
);
2921 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2924 test_true(status
== MEMCACHED_SUCCESS
);
2926 memcached_result_free(result
);
2928 return TEST_SUCCESS
;
2931 #include "ketama_test_cases.h"
2932 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2934 memcached_return_t rc
;
2937 memcached_server_st
*server_pool
;
2942 memc
= memcached_create(NULL
);
2945 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2946 test_true(rc
== MEMCACHED_SUCCESS
);
2948 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2949 test_true(value
== 1);
2951 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2952 test_true(rc
== MEMCACHED_SUCCESS
);
2954 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2955 test_true(value
== MEMCACHED_HASH_MD5
);
2957 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");
2958 memcached_server_push(memc
, server_pool
);
2960 /* verify that the server list was parsed okay. */
2961 test_true(memcached_server_count(memc
) == 8);
2962 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2963 test_true(server_pool
[0].port
== 11211);
2964 test_true(server_pool
[0].weight
== 600);
2965 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2966 test_true(server_pool
[2].port
== 11211);
2967 test_true(server_pool
[2].weight
== 200);
2968 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2969 test_true(server_pool
[7].port
== 11211);
2970 test_true(server_pool
[7].weight
== 100);
2972 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2973 * us test the boundary wraparound.
2975 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
2977 /* verify the standard ketama set. */
2978 for (x
= 0; x
< 99; x
++)
2980 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2982 memcached_server_instance_st instance
=
2983 memcached_server_instance_by_position(memc
, server_idx
);
2985 const char *hostname
= memcached_server_name(instance
);
2986 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2989 memcached_server_list_free(server_pool
);
2990 memcached_free(memc
);
2992 return TEST_SUCCESS
;
2995 /* Large mget() of missing keys with binary proto
2997 * If many binary quiet commands (such as getq's in an mget) fill the output
2998 * buffer and the server chooses not to respond, memcached_flush hangs. See
2999 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
3002 /* sighandler_t function that always asserts false */
3003 static void fail(int unused
)
3010 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
3015 return TEST_SKIPPED
;
3017 memcached_return_t rc
;
3020 size_t* key_lengths
;
3021 void (*oldalarm
)(int);
3022 memcached_st
*memc_clone
;
3024 memc_clone
= memcached_clone(NULL
, memc
);
3025 test_true(memc_clone
);
3027 /* only binproto uses getq for mget */
3028 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3030 /* empty the cache to ensure misses (hence non-responses) */
3031 rc
= memcached_flush(memc_clone
, 0);
3032 test_true(rc
== MEMCACHED_SUCCESS
);
3034 key_lengths
= calloc(key_count
, sizeof(size_t));
3035 keys
= calloc(key_count
, sizeof(char *));
3037 for (x
= 0; x
< key_count
; x
++)
3041 snprintf(buffer
, 30, "%u", x
);
3042 keys
[x
]= strdup(buffer
);
3043 key_lengths
[x
]= strlen(keys
[x
]);
3046 oldalarm
= signal(SIGALRM
, fail
);
3049 rc
= memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
);
3050 test_true(rc
== MEMCACHED_SUCCESS
);
3053 signal(SIGALRM
, oldalarm
);
3055 size_t keys_returned
;
3056 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3058 for (x
= 0; x
< key_count
; x
++)
3063 memcached_free(memc_clone
);
3065 return TEST_SUCCESS
;
3069 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3071 test_return_t test_rc
;
3072 test_rc
= pre_binary(memc
);
3074 if (test_rc
!= TEST_SUCCESS
)
3079 /* should work as of r580 */
3080 rc
= _user_supplied_bug21(memc
, 10);
3081 test_true(rc
== TEST_SUCCESS
);
3083 /* should fail as of r580 */
3084 rc
= _user_supplied_bug21(memc
, 1000);
3085 test_true(rc
== TEST_SUCCESS
);
3087 return TEST_SUCCESS
;
3090 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3093 memcached_server_instance_st instance
;
3095 memcached_return_t rc
;
3096 memcached_st
*memc
= memcached_create(NULL
);
3099 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3100 test_true(rc
== MEMCACHED_SUCCESS
);
3102 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3103 test_true(value
== 1);
3105 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3106 test_true(rc
== MEMCACHED_SUCCESS
);
3108 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3109 test_true(value
== MEMCACHED_HASH_MD5
);
3111 /* server should be removed when in delay */
3112 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3113 test_true(rc
== MEMCACHED_SUCCESS
);
3115 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3116 test_true(value
== 1);
3118 memcached_server_st
*server_pool
;
3119 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");
3120 memcached_server_push(memc
, server_pool
);
3122 /* verify that the server list was parsed okay. */
3123 test_true(memcached_server_count(memc
) == 8);
3124 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3125 test_true(server_pool
[0].port
== 11211);
3126 test_true(server_pool
[0].weight
== 600);
3127 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3128 test_true(server_pool
[2].port
== 11211);
3129 test_true(server_pool
[2].weight
== 200);
3130 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3131 test_true(server_pool
[7].port
== 11211);
3132 test_true(server_pool
[7].weight
== 100);
3134 instance
= memcached_server_instance_by_position(memc
, 2);
3135 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3136 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3139 This would not work if there were only two hosts.
3141 for (size_t x
= 0; x
< 99; x
++)
3143 memcached_autoeject(memc
);
3144 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3145 test_true(server_idx
!= 2);
3148 /* and re-added when it's back. */
3149 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3150 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3152 memc
->distribution
);
3153 for (size_t x
= 0; x
< 99; x
++)
3155 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3156 // We re-use instance from above.
3158 memcached_server_instance_by_position(memc
, server_idx
);
3159 const char *hostname
= memcached_server_name(instance
);
3160 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3163 memcached_server_list_free(server_pool
);
3164 memcached_free(memc
);
3166 return TEST_SUCCESS
;
3169 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3173 memcached_return_t rc
;
3174 memcached_st
*memc
= memcached_create(NULL
);
3178 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3179 test_true(rc
== MEMCACHED_SUCCESS
);
3181 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3182 test_true(value
== 1);
3184 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3185 test_true(rc
== MEMCACHED_SUCCESS
);
3187 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3188 test_true(value
== MEMCACHED_HASH_MD5
);
3191 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3193 memcached_server_st
*server_pool
;
3194 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");
3195 memcached_server_push(memc
, server_pool
);
3197 // @todo this needs to be refactored to actually test something.
3200 if ((fp
= fopen("ketama_keys.txt", "w")))
3204 printf("cannot write to file ketama_keys.txt");
3205 return TEST_FAILURE
;
3208 for (int x
= 0; x
< 10000; x
++)
3211 snprintf(key
, sizeof(key
), "%d", x
);
3213 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3214 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3215 in_port_t port
= memc
->hosts
[server_idx
].port
;
3216 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3217 memcached_server_instance_st instance
=
3218 memcached_server_instance_by_position(memc
, host_index
);
3222 memcached_server_list_free(server_pool
);
3223 memcached_free(memc
);
3225 return TEST_SUCCESS
;
3229 static test_return_t
result_static(memcached_st
*memc
)
3231 memcached_result_st result
;
3232 memcached_result_st
*result_ptr
;
3234 result_ptr
= memcached_result_create(memc
, &result
);
3235 test_true(result
.options
.is_allocated
== false);
3236 test_true(memcached_is_initialized(&result
) == true);
3237 test_true(result_ptr
);
3238 test_true(result_ptr
== &result
);
3240 memcached_result_free(&result
);
3242 test_true(result
.options
.is_allocated
== false);
3243 test_true(memcached_is_initialized(&result
) == false);
3245 return TEST_SUCCESS
;
3248 static test_return_t
result_alloc(memcached_st
*memc
)
3250 memcached_result_st
*result_ptr
;
3252 result_ptr
= memcached_result_create(memc
, NULL
);
3253 test_true(result_ptr
);
3254 test_true(result_ptr
->options
.is_allocated
== true);
3255 test_true(memcached_is_initialized(result_ptr
) == true);
3256 memcached_result_free(result_ptr
);
3258 return TEST_SUCCESS
;
3261 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3264 pairs_free(global_pairs
);
3266 return TEST_SUCCESS
;
3269 static test_return_t
generate_pairs(memcached_st
*memc
)
3272 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3273 global_count
= GLOBAL_COUNT
;
3275 for (size_t x
= 0; x
< global_count
; x
++)
3277 global_keys
[x
]= global_pairs
[x
].key
;
3278 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3281 return TEST_SUCCESS
;
3284 static test_return_t
generate_large_pairs(memcached_st
*memc
)
3287 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3288 global_count
= GLOBAL2_COUNT
;
3290 for (size_t x
= 0; x
< global_count
; x
++)
3292 global_keys
[x
]= global_pairs
[x
].key
;
3293 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3296 return TEST_SUCCESS
;
3299 static test_return_t
generate_data(memcached_st
*memc
)
3301 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3303 test_true(check_execute
== global_count
);
3305 return TEST_SUCCESS
;
3308 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3310 memcached_stat_st
*stat_p
;
3311 memcached_return_t rc
;
3312 uint32_t host_index
= 0;
3313 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3315 test_true(check_execute
== global_count
);
3317 //TODO: hosts used size stats
3318 stat_p
= memcached_stat(memc
, NULL
, &rc
);
3321 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3323 /* This test was changes so that "make test" would work properlly */
3325 memcached_server_instance_st instance
=
3326 memcached_server_instance_by_position(memc
, host_index
);
3328 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3330 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3333 memcached_stat_free(NULL
, stat_p
);
3335 return TEST_SUCCESS
;
3337 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3342 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3343 generate_data(memc
);
3345 return TEST_SUCCESS
;
3348 static test_return_t
get_read_count(memcached_st
*memc
)
3350 memcached_return_t rc
;
3351 memcached_st
*memc_clone
;
3353 memc_clone
= memcached_clone(NULL
, memc
);
3354 test_true(memc_clone
);
3356 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3360 size_t return_value_length
;
3364 for (size_t x
= count
= 0; x
< global_count
; x
++)
3366 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3367 &return_value_length
, &flags
, &rc
);
3368 if (rc
== MEMCACHED_SUCCESS
)
3377 memcached_free(memc_clone
);
3379 return TEST_SUCCESS
;
3382 static test_return_t
get_read(memcached_st
*memc
)
3384 memcached_return_t rc
;
3388 size_t return_value_length
;
3391 for (size_t x
= 0; x
< global_count
; x
++)
3393 return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3394 &return_value_length
, &flags
, &rc
);
3396 test_true(return_value);
3397 test_true(rc == MEMCACHED_SUCCESS);
3399 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3404 return TEST_SUCCESS
;
3407 static test_return_t
mget_read(memcached_st
*memc
)
3409 memcached_return_t rc
;
3411 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3412 return TEST_SKIPPED
;
3414 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3416 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3418 // Go fetch the keys and test to see if all of them were returned
3420 size_t keys_returned
;
3421 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3423 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3424 test_true_got(global_count
== keys_returned
, buffer
);
3428 return TEST_SUCCESS
;
3431 static test_return_t
mget_read_result(memcached_st
*memc
)
3433 memcached_return_t rc
;
3435 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3436 return TEST_SKIPPED
;
3438 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3440 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3442 /* Turn this into a help function */
3444 memcached_result_st results_obj
;
3445 memcached_result_st
*results
;
3447 results
= memcached_result_create(memc
, &results_obj
);
3449 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3452 test_true(rc
== MEMCACHED_SUCCESS
);
3455 memcached_result_free(&results_obj
);
3458 return TEST_SUCCESS
;
3461 static test_return_t
mget_read_function(memcached_st
*memc
)
3463 memcached_return_t rc
;
3465 memcached_execute_fn callbacks
[1];
3467 if (! libmemcached_util_version_check(memc
, 1, 4, 4))
3468 return TEST_SKIPPED
;
3470 rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3472 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3474 callbacks
[0]= &callback_counter
;
3476 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3478 return TEST_SUCCESS
;
3481 static test_return_t
delete_generate(memcached_st
*memc
)
3483 for (size_t x
= 0; x
< global_count
; x
++)
3485 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3488 return TEST_SUCCESS
;
3491 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3496 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3498 for (size_t x
= 0; x
< global_count
; x
++)
3500 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3503 return TEST_SUCCESS
;
3506 static test_return_t
add_host_test1(memcached_st
*memc
)
3508 memcached_return_t rc
;
3509 char servername
[]= "0.example.com";
3510 memcached_server_st
*servers
;
3512 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3514 test_true(1 == memcached_server_list_count(servers
));
3516 for (size_t x
= 2; x
< 20; x
++)
3518 char buffer
[SMALL_STRING_LEN
];
3520 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3521 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3523 test_true(rc
== MEMCACHED_SUCCESS
);
3524 test_true(x
== memcached_server_list_count(servers
));
3527 rc
= memcached_server_push(memc
, servers
);
3528 test_true(rc
== MEMCACHED_SUCCESS
);
3529 rc
= memcached_server_push(memc
, servers
);
3530 test_true(rc
== MEMCACHED_SUCCESS
);
3532 memcached_server_list_free(servers
);
3534 return TEST_SUCCESS
;
3537 static test_return_t
pre_nonblock(memcached_st
*memc
)
3539 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3541 return TEST_SUCCESS
;
3544 static test_return_t
pre_cork(memcached_st
*memc
)
3546 memcached_return_t rc
;
3549 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
3552 return TEST_SKIPPED
;
3555 if (rc
== MEMCACHED_SUCCESS
)
3556 return TEST_SUCCESS
;
3558 return TEST_SKIPPED
;
3561 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3568 return TEST_SKIPPED
;
3571 if (rc
!= TEST_SUCCESS
)
3574 return pre_nonblock(memc
);
3577 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3579 memcached_return_t rc
= MEMCACHED_FAILURE
;
3580 memcached_st
*memc_clone
;
3582 memc_clone
= memcached_clone(NULL
, memc
);
3583 test_true(memc_clone
);
3584 // The memcached_version needs to be done on a clone, because the server
3585 // will not toggle protocol on an connection.
3586 memcached_version(memc_clone
);
3588 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3590 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3591 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3592 test_true(rc
== MEMCACHED_SUCCESS
);
3593 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3597 return TEST_SKIPPED
;
3600 memcached_free(memc_clone
);
3602 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3605 static test_return_t
pre_murmur(memcached_st
*memc
)
3607 #ifdef HAVE_MURMUR_HASH
3608 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3609 return TEST_SUCCESS
;
3612 return TEST_SKIPPED
;
3616 static test_return_t
pre_jenkins(memcached_st
*memc
)
3618 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3620 return TEST_SUCCESS
;
3624 static test_return_t
pre_md5(memcached_st
*memc
)
3626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3628 return TEST_SUCCESS
;
3631 static test_return_t
pre_crc(memcached_st
*memc
)
3633 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3635 return TEST_SUCCESS
;
3638 static test_return_t
pre_hsieh(memcached_st
*memc
)
3640 #ifdef HAVE_HSIEH_HASH
3641 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3642 return TEST_SUCCESS
;
3645 return TEST_SKIPPED
;
3649 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3651 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3653 return TEST_SUCCESS
;
3656 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3658 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3660 return TEST_SUCCESS
;
3663 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3667 return TEST_SUCCESS
;
3670 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3672 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3674 return TEST_SUCCESS
;
3677 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3679 memcached_return_t rc
;
3682 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3683 test_true(rc
== MEMCACHED_SUCCESS
);
3685 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3686 test_true(value
== 1);
3688 return TEST_SUCCESS
;
3691 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3693 memcached_return_t rc
;
3696 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3697 test_true(rc
== MEMCACHED_SUCCESS
);
3699 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3700 test_true(value
== 1);
3702 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3703 test_true(rc
== MEMCACHED_SUCCESS
);
3705 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3706 test_true(value
== MEMCACHED_HASH_MD5
);
3708 return TEST_SUCCESS
;
3712 @note This should be testing to see if the server really supports the binary protocol.
3714 static test_return_t
pre_binary(memcached_st
*memc
)
3716 memcached_return_t rc
= MEMCACHED_FAILURE
;
3718 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3720 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3721 test_true(rc
== MEMCACHED_SUCCESS
);
3722 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3725 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3728 static test_return_t
pre_sasl(memcached_st
*memc
)
3730 memcached_return_t rc
= MEMCACHED_FAILURE
;
3732 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3733 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3734 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3735 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3737 if (server
!= NULL
&& user
!= NULL
&& pass
!= NULL
)
3739 memcached_server_st
*servers
= memcached_servers_parse(server
);
3740 test_true(servers
!= NULL
);
3741 memcached_servers_reset(memc
);
3742 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3743 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3744 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3745 test_true(rc
== MEMCACHED_SUCCESS
);
3751 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3754 static test_return_t
pre_replication(memcached_st
*memc
)
3756 test_return_t test_rc
;
3757 test_rc
= pre_binary(memc
);
3759 if (test_rc
!= TEST_SUCCESS
)
3763 * Make sure that we store the item on all servers
3764 * (master + replicas == number of servers)
3766 memcached_return_t rc
;
3767 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3768 memcached_server_count(memc
) - 1);
3769 test_true(rc
== MEMCACHED_SUCCESS
);
3770 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3772 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3776 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3780 rc
= pre_replication(memc
);
3781 if (rc
!= TEST_SUCCESS
)
3784 rc
= pre_nonblock(memc
);
3790 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3794 #ifdef HARD_MALLOC_TESTS
3795 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3803 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3807 #ifdef HARD_MALLOC_TESTS
3808 void *ret
= malloc(size
+ 8);
3811 ret
= (void*)((caddr_t
)ret
+ 8);
3814 void *ret
= malloc(size
);
3819 memset(ret
, 0xff, size
);
3826 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *context
)
3829 #ifdef HARD_MALLOC_TESTS
3830 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3831 void *nmem
= realloc(real_ptr
, size
+ 8);
3836 ret
= (void*)((caddr_t
)nmem
+ 8);
3842 return realloc(mem
, size
);
3847 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *context
)
3850 #ifdef HARD_MALLOC_TESTS
3851 void *mem
= my_malloc(ptr
, nelem
* size
);
3854 memset(mem
, 0, nelem
* size
);
3860 return calloc(nelem
, size
);
3865 static test_return_t
set_prefix(memcached_st
*memc
)
3867 memcached_return_t rc
;
3868 const char *key
= "mine";
3871 /* Make sure be default none exists */
3872 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3873 test_true(rc
== MEMCACHED_FAILURE
);
3875 /* Test a clean set */
3876 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3877 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3879 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3881 test_true(memcmp(value
, key
, 4) == 0);
3882 test_true(rc
== MEMCACHED_SUCCESS
);
3884 /* Test that we can turn it off */
3885 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3886 test_true(rc
== MEMCACHED_SUCCESS
);
3888 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3890 test_true(rc
== MEMCACHED_FAILURE
);
3892 /* Now setup for main test */
3893 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3894 test_true(rc
== MEMCACHED_SUCCESS
);
3896 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3898 test_true(rc
== MEMCACHED_SUCCESS
);
3899 test_true(memcmp(value
, key
, 4) == 0);
3901 /* Set to Zero, and then Set to something too large */
3904 memset(long_key
, 0, 255);
3906 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3907 test_true(rc
== MEMCACHED_SUCCESS
);
3909 value
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3911 test_true(rc
== MEMCACHED_FAILURE
);
3912 test_true(value
== NULL
);
3914 /* Test a long key for failure */
3915 /* TODO, extend test to determine based on setting, what result should be */
3916 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3917 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3918 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3919 test_true(rc
== MEMCACHED_SUCCESS
);
3921 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3922 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3923 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3924 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3926 /* Test for a bad prefix, but with a short key */
3927 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3928 test_true(rc
== MEMCACHED_SUCCESS
);
3930 strncpy(long_key
, "dog cat", sizeof(long_key
));
3931 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3932 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3935 return TEST_SUCCESS
;
3939 #ifdef MEMCACHED_ENABLE_DEPRECATED
3940 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3942 void *test_ptr
= NULL
;
3945 memcached_malloc_fn malloc_cb
=
3946 (memcached_malloc_fn
)my_malloc
;
3947 cb_ptr
= *(void **)&malloc_cb
;
3948 memcached_return_t rc
;
3950 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3951 test_true(rc
== MEMCACHED_SUCCESS
);
3952 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3953 test_true(rc
== MEMCACHED_SUCCESS
);
3954 test_true(test_ptr
== cb_ptr
);
3958 memcached_realloc_fn realloc_cb
=
3959 (memcached_realloc_fn
)my_realloc
;
3960 cb_ptr
= *(void **)&realloc_cb
;
3961 memcached_return_t rc
;
3963 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3964 test_true(rc
== MEMCACHED_SUCCESS
);
3965 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3966 test_true(rc
== MEMCACHED_SUCCESS
);
3967 test_true(test_ptr
== cb_ptr
);
3971 memcached_free_fn free_cb
=
3972 (memcached_free_fn
)my_free
;
3973 cb_ptr
= *(void **)&free_cb
;
3974 memcached_return_t rc
;
3976 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3977 test_true(rc
== MEMCACHED_SUCCESS
);
3978 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3979 test_true(rc
== MEMCACHED_SUCCESS
);
3980 test_true(test_ptr
== cb_ptr
);
3983 return TEST_SUCCESS
;
3988 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3990 memcached_return_t rc
;
3991 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3992 my_realloc
, my_calloc
, NULL
);
3993 test_true(rc
== MEMCACHED_FAILURE
);
3995 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3996 my_realloc
, my_calloc
, NULL
);
3998 memcached_malloc_fn mem_malloc
;
3999 memcached_free_fn mem_free
;
4000 memcached_realloc_fn mem_realloc
;
4001 memcached_calloc_fn mem_calloc
;
4002 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
4003 &mem_realloc
, &mem_calloc
);
4005 test_true(mem_malloc
== my_malloc
);
4006 test_true(mem_realloc
== my_realloc
);
4007 test_true(mem_calloc
== my_calloc
);
4008 test_true(mem_free
== my_free
);
4010 return TEST_SUCCESS
;
4013 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
4016 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4017 memcached_hash_t hash
;
4018 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4019 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
4022 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4023 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4025 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4027 if (hash
!= MEMCACHED_HASH_CRC
)
4028 return TEST_SKIPPED
;
4030 return TEST_SUCCESS
;
4033 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
4036 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
4037 memcached_hash_t hash
;
4038 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
4039 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
4042 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
4043 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
4045 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
4047 if (hash
!= MEMCACHED_HASH_HSIEH
)
4048 return TEST_SKIPPED
;
4051 return TEST_SUCCESS
;
4054 static test_return_t
enable_cas(memcached_st
*memc
)
4056 unsigned int set
= 1;
4058 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4060 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4062 return TEST_SUCCESS
;
4065 return TEST_SKIPPED
;
4068 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4070 memcached_version(memc
);
4072 memcached_server_instance_st instance
=
4073 memcached_server_instance_by_position(memc
, 0);
4075 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4076 || instance
->minor_version
> 2)
4078 return TEST_SUCCESS
;
4081 return TEST_SKIPPED
;
4084 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4086 memcached_return_t rc
;
4089 memcached_servers_reset(memc
);
4091 if (stat("/tmp/memcached.socket", &buf
))
4092 return TEST_SKIPPED
;
4094 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4096 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4099 static test_return_t
pre_nodelay(memcached_st
*memc
)
4101 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4102 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4104 return TEST_SUCCESS
;
4107 static test_return_t
pre_settimer(memcached_st
*memc
)
4109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4110 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4112 return TEST_SUCCESS
;
4115 static test_return_t
poll_timeout(memcached_st
*memc
)
4121 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4123 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4125 test_true(timeout
== 100);
4127 return TEST_SUCCESS
;
4130 static test_return_t
noreply_test(memcached_st
*memc
)
4132 memcached_return_t ret
;
4133 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4134 test_true(ret
== MEMCACHED_SUCCESS
);
4135 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4136 test_true(ret
== MEMCACHED_SUCCESS
);
4137 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4138 test_true(ret
== MEMCACHED_SUCCESS
);
4139 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4140 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4141 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4143 for (int count
=0; count
< 5; ++count
)
4145 for (size_t x
= 0; x
< 100; ++x
)
4148 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4149 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4151 size_t len
= (size_t)check_length
;
4156 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4159 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4162 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4165 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4168 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4174 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4178 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4179 ** API and is _ONLY_ done this way to verify that the library works the
4180 ** way it is supposed to do!!!!
4183 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4185 memcached_server_instance_st instance
=
4186 memcached_server_instance_by_position(memc
, x
);
4187 no_msg
+=(int)(instance
->cursor_active
);
4190 test_true(no_msg
== 0);
4191 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4194 ** Now validate that all items was set properly!
4196 for (size_t x
= 0; x
< 100; ++x
)
4200 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4202 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4204 size_t len
= (size_t)check_length
;
4207 char* value
=memcached_get(memc
, key
, strlen(key
),
4208 &length
, &flags
, &ret
);
4209 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4212 case 0: /* FALLTHROUGH */
4213 case 1: /* FALLTHROUGH */
4215 test_true(strncmp(value
, key
, len
) == 0);
4216 test_true(len
== length
);
4219 test_true(length
== len
* 2);
4222 test_true(length
== len
* 3);
4232 /* Try setting an illegal cas value (should not return an error to
4233 * the caller (because we don't expect a return message from the server)
4235 const char* keys
[]= {"0"};
4236 size_t lengths
[]= {1};
4239 memcached_result_st results_obj
;
4240 memcached_result_st
*results
;
4241 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4242 test_true(ret
== MEMCACHED_SUCCESS
);
4244 results
= memcached_result_create(memc
, &results_obj
);
4246 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4248 test_true(ret
== MEMCACHED_SUCCESS
);
4249 uint64_t cas
= memcached_result_cas(results
);
4250 memcached_result_free(&results_obj
);
4252 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4253 test_true(ret
== MEMCACHED_SUCCESS
);
4256 * The item will have a new cas value, so try to set it again with the old
4257 * value. This should fail!
4259 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4260 test_true(ret
== MEMCACHED_SUCCESS
);
4261 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4262 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4263 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4266 return TEST_SUCCESS
;
4269 static test_return_t
analyzer_test(memcached_st
*memc
)
4271 memcached_return_t rc
;
4272 memcached_stat_st
*memc_stat
;
4273 memcached_analysis_st
*report
;
4275 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4276 test_true(rc
== MEMCACHED_SUCCESS
);
4277 test_true(memc_stat
);
4279 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4280 test_true(rc
== MEMCACHED_SUCCESS
);
4284 memcached_stat_free(NULL
, memc_stat
);
4286 return TEST_SUCCESS
;
4289 /* Count the objects */
4290 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4295 (void)ptr
; (void)key
; (void)key_length
;
4296 size_t *counter
= (size_t *)context
;
4298 *counter
= *counter
+ 1;
4300 return MEMCACHED_SUCCESS
;
4303 static test_return_t
dump_test(memcached_st
*memc
)
4305 memcached_return_t rc
;
4307 memcached_dump_fn callbacks
[1];
4308 test_return_t main_rc
;
4310 callbacks
[0]= &callback_dump_counter
;
4312 /* No support for Binary protocol yet */
4313 if (memc
->flags
.binary_protocol
)
4314 return TEST_SUCCESS
;
4316 main_rc
= set_test3(memc
);
4318 test_true (main_rc
== TEST_SUCCESS
);
4320 rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4321 test_true(rc
== MEMCACHED_SUCCESS
);
4323 /* We may have more then 32 if our previous flush has not completed */
4324 test_true(counter
>= 32);
4326 return TEST_SUCCESS
;
4329 #ifdef HAVE_LIBMEMCACHEDUTIL
4330 static void* connection_release(void *arg
)
4333 memcached_pool_st
* pool
;
4338 // Release all of the memc we are holding
4339 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4343 #define POOL_SIZE 10
4344 static test_return_t
connection_pool_test(memcached_st
*memc
)
4346 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4347 test_true(pool
!= NULL
);
4348 memcached_st
*mmc
[POOL_SIZE
];
4349 memcached_return_t rc
;
4351 // Fill up our array that we will store the memc that are in the pool
4352 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4354 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4355 test_true(mmc
[x
] != NULL
);
4356 test_true(rc
== MEMCACHED_SUCCESS
);
4359 // All memc should be gone
4360 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4361 test_true(rc
== MEMCACHED_SUCCESS
);
4365 memcached_pool_st
* pool
;
4367 } item
= { .pool
= pool
, .mmc
= mmc
[9] };
4369 pthread_create(&tid
, NULL
, connection_release
, &item
);
4370 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4371 test_true(rc
== MEMCACHED_SUCCESS
);
4372 pthread_join(tid
, NULL
);
4374 const char *key
= "key";
4375 size_t keylen
= strlen(key
);
4377 // verify that I can do ops with all connections
4378 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4379 test_true(rc
== MEMCACHED_SUCCESS
);
4381 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4383 uint64_t number_value
;
4384 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4385 test_true(rc
== MEMCACHED_SUCCESS
);
4386 test_true(number_value
== (x
+1));
4390 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4392 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4396 /* verify that I can set behaviors on the pool when I don't have all
4397 * of the connections in the pool. It should however be enabled
4398 * when I push the item into the pool
4400 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4401 test_true(mmc
[0] != NULL
);
4403 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4404 test_true(rc
== MEMCACHED_SUCCESS
);
4406 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4407 test_true(mmc
[1] != NULL
);
4409 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4410 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4411 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4413 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4414 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4415 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4417 test_true(memcached_pool_destroy(pool
) == memc
);
4419 return TEST_SUCCESS
;
4422 static test_return_t
util_version_test(memcached_st
*memc
)
4426 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4427 test_true(if_successful
== true);
4429 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4431 // We expect failure
4434 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4435 fprintf(stderr
, "\nDumping Server Information\n\n");
4436 memcached_server_fn callbacks
[1];
4438 callbacks
[0]= dump_server_information
;
4439 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4440 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4442 test_true(if_successful
== false);
4444 memcached_server_instance_st instance
=
4445 memcached_server_instance_by_position(memc
, 0);
4447 memcached_version(memc
);
4449 // We only use one binary when we test, so this should be just fine.
4450 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4451 test_true(if_successful
== true);
4453 if (instance
->micro_version
> 0)
4454 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4455 else if (instance
->minor_version
> 0)
4456 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4457 else if (instance
->major_version
> 0)
4458 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4460 test_true(if_successful
== true);
4462 if (instance
->micro_version
> 0)
4463 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4464 else if (instance
->minor_version
> 0)
4465 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4466 else if (instance
->major_version
> 0)
4467 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4469 test_true(if_successful
== false);
4471 return TEST_SUCCESS
;
4474 static test_return_t
ping_test(memcached_st
*memc
)
4476 memcached_return_t rc
;
4477 memcached_server_instance_st instance
=
4478 memcached_server_instance_by_position(memc
, 0);
4480 // Test both the version that returns a code, and the one that does not.
4481 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4482 memcached_server_port(instance
), NULL
));
4484 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4485 memcached_server_port(instance
), &rc
));
4487 test_true(rc
== MEMCACHED_SUCCESS
);
4489 return TEST_SUCCESS
;
4495 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4499 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4500 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4501 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4502 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4503 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4504 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4505 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4506 #ifdef HAVE_HSIEH_HASH
4507 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4509 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4510 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4511 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4513 return TEST_SUCCESS
;
4517 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4519 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4520 #ifdef HAVE_HSIEH_HASH
4521 expected_rc
= MEMCACHED_SUCCESS
;
4523 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4524 (uint64_t)MEMCACHED_HASH_HSIEH
);
4525 test_true(rc
== expected_rc
);
4527 return TEST_SUCCESS
;
4530 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4532 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4533 #ifdef HAVE_MURMUR_HASH
4534 expected_rc
= MEMCACHED_SUCCESS
;
4536 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4537 (uint64_t)MEMCACHED_HASH_MURMUR
);
4538 test_true(rc
== expected_rc
);
4540 return TEST_SUCCESS
;
4543 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4549 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4553 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4554 test_true(one_at_a_time_values
[x
] == hash_val
);
4557 return TEST_SUCCESS
;
4560 static test_return_t
md5_run (memcached_st
*memc
)
4566 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4570 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4571 test_true(md5_values
[x
] == hash_val
);
4574 return TEST_SUCCESS
;
4577 static test_return_t
crc_run (memcached_st
*memc
)
4583 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4587 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4588 test_true(crc_values
[x
] == hash_val
);
4591 return TEST_SUCCESS
;
4594 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4600 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4604 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4605 test_true(fnv1_64_values
[x
] == hash_val
);
4608 return TEST_SUCCESS
;
4611 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4617 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4621 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4622 test_true(fnv1a_64_values
[x
] == hash_val
);
4625 return TEST_SUCCESS
;
4628 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4634 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4638 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4639 test_true(fnv1_32_values
[x
] == hash_val
);
4642 return TEST_SUCCESS
;
4645 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4651 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4655 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4656 test_true(fnv1a_32_values
[x
] == hash_val
);
4659 return TEST_SUCCESS
;
4662 static test_return_t
hsieh_run (memcached_st
*memc
)
4668 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4672 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4673 test_true(hsieh_values
[x
] == hash_val
);
4676 return TEST_SUCCESS
;
4679 static test_return_t
murmur_run (memcached_st
*memc
)
4681 #ifdef WORDS_BIGENDIAN
4682 (void)murmur_values
;
4683 return TEST_SKIPPED
;
4689 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4693 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4694 test_true(murmur_values
[x
] == hash_val
);
4697 return TEST_SUCCESS
;
4701 static test_return_t
jenkins_run (memcached_st
*memc
)
4707 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4711 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4712 test_true(jenkins_values
[x
] == hash_val
);
4715 return TEST_SUCCESS
;
4718 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4721 return libhashkit_md5(string
, string_length
);
4724 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4727 return libhashkit_crc32(string
, string_length
);
4730 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4734 const hashkit_st
*kit
;
4736 hashkit_return_t hash_rc
;
4738 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};
4739 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};
4741 kit
= memcached_get_hashkit(memc
);
4743 hashkit_clone(&new_kit
, kit
);
4744 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4745 test_true(hash_rc
== HASHKIT_SUCCESS
);
4747 memcached_set_hashkit(memc
, &new_kit
);
4750 Verify Setting the hash.
4752 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4756 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4757 test_true(md5_values
[x
] == hash_val
);
4762 Now check memcached_st.
4764 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4768 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4769 test_true(md5_hosts
[x
] == hash_val
);
4772 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4773 test_true(hash_rc
== HASHKIT_SUCCESS
);
4775 memcached_set_hashkit(memc
, &new_kit
);
4778 Verify Setting the hash.
4780 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4784 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4785 test_true(crc_values
[x
] == hash_val
);
4788 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4792 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4793 test_true(crc_hosts
[x
] == hash_val
);
4796 return TEST_SUCCESS
;
4800 Test case adapted from John Gorman <johngorman2@gmail.com>
4802 We are testing the error condition when we connect to a server via memcached_get()
4803 but find that the server is not available.
4805 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4808 memcached_st
*tl_memc_h
;
4809 memcached_server_st
*servers
;
4811 const char *key
= "MemcachedLives";
4814 memcached_return rc
;
4818 tl_memc_h
= memcached_create(NULL
);
4819 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4820 memcached_server_push(tl_memc_h
, servers
);
4821 memcached_server_list_free(servers
);
4823 // See if memcached is reachable.
4824 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4827 test_true(len
== 0);
4828 test_false(rc
== MEMCACHED_SUCCESS
);
4830 memcached_free(tl_memc_h
);
4832 return TEST_SUCCESS
;
4836 We connect to a server which exists, but search for a key that does not exist.
4838 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4840 const char *key
= "MemcachedKeyNotEXIST";
4843 memcached_return rc
;
4846 // See if memcached is reachable.
4847 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4850 test_true(len
== 0);
4851 test_true(rc
== MEMCACHED_NOTFOUND
);
4853 return TEST_SUCCESS
;
4857 Test case adapted from John Gorman <johngorman2@gmail.com>
4859 We are testing the error condition when we connect to a server via memcached_get_by_key()
4860 but find that the server is not available.
4862 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4865 memcached_st
*tl_memc_h
;
4866 memcached_server_st
*servers
;
4868 const char *key
= "MemcachedLives";
4871 memcached_return rc
;
4875 tl_memc_h
= memcached_create(NULL
);
4876 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4877 memcached_server_push(tl_memc_h
, servers
);
4878 memcached_server_list_free(servers
);
4880 // See if memcached is reachable.
4881 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4884 test_true(len
== 0);
4885 test_false(rc
== MEMCACHED_SUCCESS
);
4887 memcached_free(tl_memc_h
);
4889 return TEST_SUCCESS
;
4893 We connect to a server which exists, but search for a key that does not exist.
4895 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4897 const char *key
= "MemcachedKeyNotEXIST";
4900 memcached_return rc
;
4903 // See if memcached is reachable.
4904 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4907 test_true(len
== 0);
4908 test_true(rc
== MEMCACHED_NOTFOUND
);
4910 return TEST_SUCCESS
;
4914 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4916 memcached_return_t rc
;
4919 memcached_server_st
*server_pool
;
4924 memc
= memcached_create(NULL
);
4927 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4928 test_true(rc
== MEMCACHED_SUCCESS
);
4930 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4931 test_true(value
== 1);
4933 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4934 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4937 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");
4938 memcached_server_push(memc
, server_pool
);
4940 /* verify that the server list was parsed okay. */
4941 test_true(memcached_server_count(memc
) == 8);
4942 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4943 test_true(server_pool
[0].port
== 11211);
4944 test_true(server_pool
[0].weight
== 600);
4945 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4946 test_true(server_pool
[2].port
== 11211);
4947 test_true(server_pool
[2].weight
== 200);
4948 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4949 test_true(server_pool
[7].port
== 11211);
4950 test_true(server_pool
[7].weight
== 100);
4952 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4953 * us test the boundary wraparound.
4955 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4957 /* verify the standard ketama set. */
4958 for (x
= 0; x
< 99; x
++)
4960 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4961 memcached_server_instance_st instance
=
4962 memcached_server_instance_by_position(memc
, server_idx
);
4963 const char *hostname
= memcached_server_name(instance
);
4965 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4968 memcached_server_list_free(server_pool
);
4969 memcached_free(memc
);
4971 return TEST_SUCCESS
;
4974 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4976 memcached_return_t rc
;
4978 memcached_server_st
*server_pool
;
4983 memc
= memcached_create(NULL
);
4986 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4987 test_true(rc
== MEMCACHED_SUCCESS
);
4989 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4990 test_true(value
== 1);
4992 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4993 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4995 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");
4996 assert(server_pool
);
4997 memcached_server_push(memc
, server_pool
);
4999 /* verify that the server list was parsed okay. */
5000 test_true(memcached_server_count(memc
) == 8);
5001 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
5002 test_true(server_pool
[0].port
== 11211);
5003 test_true(server_pool
[0].weight
== 600);
5004 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
5005 test_true(server_pool
[2].port
== 11211);
5006 test_true(server_pool
[2].weight
== 200);
5007 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
5008 test_true(server_pool
[7].port
== 11211);
5009 test_true(server_pool
[7].weight
== 100);
5011 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
5012 * us test the boundary wraparound.
5014 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
5016 /* verify the standard ketama set. */
5017 for (uint32_t x
= 0; x
< 99; x
++)
5019 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
5021 memcached_server_instance_st instance
=
5022 memcached_server_instance_by_position(memc
, server_idx
);
5024 const char *hostname
= memcached_server_name(instance
);
5026 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
5029 memcached_server_list_free(server_pool
);
5030 memcached_free(memc
);
5032 return TEST_SUCCESS
;
5035 static test_return_t
regression_bug_434484(memcached_st
*memc
)
5037 test_return_t test_rc
;
5038 test_rc
= pre_binary(memc
);
5040 if (test_rc
!= TEST_SUCCESS
)
5043 memcached_return_t ret
;
5044 const char *key
= "regression_bug_434484";
5045 size_t keylen
= strlen(key
);
5047 ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
5048 test_true(ret
== MEMCACHED_NOTSTORED
);
5050 size_t size
= 2048 * 1024;
5051 void *data
= calloc(1, size
);
5052 test_true(data
!= NULL
);
5053 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
5054 test_true(ret
== MEMCACHED_E2BIG
);
5057 return TEST_SUCCESS
;
5060 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5062 test_return_t test_rc
;
5063 test_rc
= pre_binary(memc
);
5065 if (test_rc
!= TEST_SUCCESS
)
5068 memcached_return_t rc
;
5070 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5073 * I only want to hit only _one_ server so I know the number of requests I'm
5074 * sending in the pipleine to the server. Let's try to do a multiget of
5075 * 1024 (that should satisfy most users don't you think?). Future versions
5076 * will include a mget_execute function call if you need a higher number.
5078 uint32_t number_of_hosts
= memcached_server_count(memc
);
5079 memc
->number_of_hosts
= 1;
5080 const size_t max_keys
= 1024;
5081 char **keys
= calloc(max_keys
, sizeof(char*));
5082 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5084 for (size_t x
= 0; x
< max_keys
; ++x
)
5088 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5090 test_true(keys
[x
] != NULL
);
5094 * Run two times.. the first time we should have 100% cache miss,
5095 * and the second time we should have 100% cache hits
5097 for (size_t y
= 0; y
< 2; y
++)
5099 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5100 test_true(rc
== MEMCACHED_SUCCESS
);
5101 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5105 /* The first iteration should give me a 100% cache miss. verify that*/
5106 char blob
[1024]= { 0 };
5108 test_true(counter
== 0);
5110 for (size_t x
= 0; x
< max_keys
; ++x
)
5112 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5113 blob
, sizeof(blob
), 0, 0);
5114 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5119 /* Verify that we received all of the key/value pairs */
5120 test_true(counter
== max_keys
);
5124 /* Release allocated resources */
5125 for (size_t x
= 0; x
< max_keys
; ++x
)
5132 memc
->number_of_hosts
= number_of_hosts
;
5134 return TEST_SUCCESS
;
5137 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5139 memcached_return_t rc
;
5140 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5141 test_true(rc
== MEMCACHED_SUCCESS
);
5143 return regression_bug_434843(memc
);
5146 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5148 memcached_return_t rc
;
5149 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5150 test_true(rc
== MEMCACHED_SUCCESS
);
5152 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5153 test_true(rc
== MEMCACHED_SUCCESS
);
5154 test_true(bytes
!= NULL
);
5155 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5157 test_true(rc
== MEMCACHED_SUCCESS
);
5158 test_true(bytes_read
!= NULL
);
5160 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5161 "bytes_written", &rc
);
5162 test_true(rc
== MEMCACHED_SUCCESS
);
5163 test_true(bytes_written
!= NULL
);
5165 test_true(strcmp(bytes
, bytes_read
) != 0);
5166 test_true(strcmp(bytes
, bytes_written
) != 0);
5168 /* Release allocated resources */
5171 free(bytes_written
);
5172 memcached_stat_free(NULL
, memc_stat
);
5174 return TEST_SUCCESS
;
5178 * The test case isn't obvious so I should probably document why
5179 * it works the way it does. Bug 442914 was caused by a bug
5180 * in the logic in memcached_purge (it did not handle the case
5181 * where the number of bytes sent was equal to the watermark).
5182 * In this test case, create messages so that we hit that case
5183 * and then disable noreply mode and issue a new command to
5184 * verify that it isn't stuck. If we change the format for the
5185 * delete command or the watermarks, we need to update this
5188 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5190 memcached_return_t rc
;
5191 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5192 test_true(rc
== MEMCACHED_SUCCESS
);
5193 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5195 uint32_t number_of_hosts
= memcached_server_count(memc
);
5196 memc
->number_of_hosts
= 1;
5201 for (uint32_t x
= 0; x
< 250; ++x
)
5203 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5204 rc
= memcached_delete(memc
, k
, len
, 0);
5205 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5208 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5211 rc
= memcached_delete(memc
, k
, len
, 0);
5212 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5214 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5215 test_true(rc
== MEMCACHED_SUCCESS
);
5216 rc
= memcached_delete(memc
, k
, len
, 0);
5217 test_true(rc
== MEMCACHED_NOTFOUND
);
5219 memc
->number_of_hosts
= number_of_hosts
;
5221 return TEST_SUCCESS
;
5224 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5226 memcached_server_instance_st instance_one
;
5227 memcached_server_instance_st instance_two
;
5229 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5230 return TEST_SKIPPED
;
5232 memcached_return_t rc
;
5234 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5235 test_true(rc
== MEMCACHED_SUCCESS
);
5237 const size_t max_keys
= 100;
5238 char **keys
= calloc(max_keys
, sizeof(char*));
5239 size_t *key_length
= calloc(max_keys
, sizeof(size_t));
5241 for (size_t x
= 0; x
< max_keys
; ++x
)
5245 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5247 test_true(keys
[x
] != NULL
);
5248 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5249 test_true(rc
== MEMCACHED_SUCCESS
);
5253 ** We are using the quiet commands to store the replicas, so we need
5254 ** to ensure that all of them are processed before we can continue.
5255 ** In the test we go directly from storing the object to trying to
5256 ** receive the object from all of the different servers, so we
5257 ** could end up in a race condition (the memcached server hasn't yet
5258 ** processed the quiet command from the replication set when it process
5259 ** the request from the other client (created by the clone)). As a
5260 ** workaround for that we call memcached_quit to send the quit command
5261 ** to the server and wait for the response ;-) If you use the test code
5262 ** as an example for your own code, please note that you shouldn't need
5265 memcached_quit(memc
);
5267 /* Verify that all messages are stored, and we didn't stuff too much
5270 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5271 test_true(rc
== MEMCACHED_SUCCESS
);
5274 memcached_execute_fn callbacks
[1]= { [0]= &callback_counter
};
5275 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5276 /* Verify that we received all of the key/value pairs */
5277 test_true(counter
== max_keys
);
5279 memcached_quit(memc
);
5281 * Don't do the following in your code. I am abusing the internal details
5282 * within the library, and this is not a supported interface.
5283 * This is to verify correct behavior in the library. Fake that two servers
5286 instance_one
= memcached_server_instance_by_position(memc
, 0);
5287 instance_two
= memcached_server_instance_by_position(memc
, 2);
5288 in_port_t port0
= instance_one
->port
;
5289 in_port_t port2
= instance_two
->port
;
5291 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5292 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5294 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5295 test_true(rc
== MEMCACHED_SUCCESS
);
5298 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5299 test_true(counter
== (unsigned int)max_keys
);
5301 /* restore the memc handle */
5302 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5303 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5305 memcached_quit(memc
);
5307 /* Remove half of the objects */
5308 for (size_t x
= 0; x
< max_keys
; ++x
)
5312 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5313 test_true(rc
== MEMCACHED_SUCCESS
);
5317 memcached_quit(memc
);
5318 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5319 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5321 /* now retry the command, this time we should have cache misses */
5322 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5323 test_true(rc
== MEMCACHED_SUCCESS
);
5326 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5327 test_true(counter
== (unsigned int)(max_keys
>> 1));
5329 /* Release allocated resources */
5330 for (size_t x
= 0; x
< max_keys
; ++x
)
5337 /* restore the memc handle */
5338 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5339 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5341 return TEST_SUCCESS
;
5344 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5346 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5347 test_true(memc_clone
!= NULL
);
5348 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5350 memcached_server_instance_st instance
=
5351 memcached_server_instance_by_position(memc_clone
, 0);
5353 if (instance
->major_version
> 1 ||
5354 (instance
->major_version
== 1 &&
5355 instance
->minor_version
> 2))
5357 /* Binary protocol doesn't support deferred delete */
5358 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5359 test_true(bin_clone
!= NULL
);
5360 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5361 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5362 memcached_free(bin_clone
);
5364 memcached_quit(memc_clone
);
5366 /* If we know the server version, deferred delete should fail
5367 * with invalid arguments */
5368 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5370 /* If we don't know the server version, we should get a protocol error */
5371 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5373 /* but there is a bug in some of the memcached servers (1.4) that treats
5374 * the counter as noreply so it doesn't send the proper error message
5376 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5378 /* And buffered mode should be disabled and we should get protocol error */
5379 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5380 rc
= memcached_delete(memc
, "foo", 3, 1);
5381 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5383 /* Same goes for noreply... */
5384 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5385 rc
= memcached_delete(memc
, "foo", 3, 1);
5386 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5388 /* but a normal request should go through (and be buffered) */
5389 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5390 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5392 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5393 /* unbuffered noreply should be success */
5394 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5395 /* unbuffered with reply should be not found... */
5396 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5397 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5400 memcached_free(memc_clone
);
5401 return TEST_SUCCESS
;
5405 /* Test memcached_server_get_last_disconnect
5406 * For a working server set, shall be NULL
5407 * For a set of non existing server, shall not be NULL
5409 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5411 memcached_return_t rc
;
5412 memcached_server_instance_st disconnected_server
;
5414 /* With the working set of server */
5415 const char *key
= "marmotte";
5416 const char *value
= "milka";
5418 memcached_reset_last_disconnected_server(memc
);
5419 rc
= memcached_set(memc
, key
, strlen(key
),
5420 value
, strlen(value
),
5421 (time_t)0, (uint32_t)0);
5422 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5424 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5425 test_true(disconnected_server
== NULL
);
5427 /* With a non existing server */
5429 memcached_server_st
*servers
;
5431 const char *server_list
= "localhost:9";
5433 servers
= memcached_servers_parse(server_list
);
5435 mine
= memcached_create(NULL
);
5436 rc
= memcached_server_push(mine
, servers
);
5437 test_true(rc
== MEMCACHED_SUCCESS
);
5438 memcached_server_list_free(servers
);
5441 rc
= memcached_set(mine
, key
, strlen(key
),
5442 value
, strlen(value
),
5443 (time_t)0, (uint32_t)0);
5444 test_true(rc
!= MEMCACHED_SUCCESS
);
5446 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5447 if (disconnected_server
== NULL
)
5449 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5452 test_true(disconnected_server
!= NULL
);
5453 test_true(memcached_server_port(disconnected_server
)== 9);
5454 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5456 memcached_quit(mine
);
5457 memcached_free(mine
);
5459 return TEST_SUCCESS
;
5462 static test_return_t
test_verbosity(memcached_st
*memc
)
5464 memcached_verbosity(memc
, 3);
5466 return TEST_SUCCESS
;
5469 static test_return_t
test_server_failure(memcached_st
*memc
)
5471 memcached_st
*local_memc
;
5472 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5474 local_memc
= memcached_create(NULL
);
5476 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5477 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5479 uint32_t server_count
= memcached_server_count(local_memc
);
5481 test_true(server_count
== 1);
5483 // Disable the server
5484 instance
= memcached_server_instance_by_position(local_memc
, 0);
5485 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5487 memcached_return_t rc
;
5488 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5490 (time_t)0, (uint32_t)0);
5491 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5493 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5494 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5496 (time_t)0, (uint32_t)0);
5497 test_true(rc
== MEMCACHED_SUCCESS
);
5500 memcached_free(local_memc
);
5502 return TEST_SUCCESS
;
5505 static test_return_t
test_cull_servers(memcached_st
*memc
)
5507 uint32_t count
= memcached_server_count(memc
);
5509 // Do not do this in your code, it is not supported.
5510 memc
->servers
[1].options
.is_dead
= true;
5511 memc
->state
.is_time_for_rebuild
= true;
5513 uint32_t new_count
= memcached_server_count(memc
);
5514 test_true(count
== new_count
);
5517 test_true(count
== new_count
+ 1 );
5520 return TEST_SUCCESS
;
5524 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5525 const char *key
, size_t key_length
,
5526 const char *value
, size_t value_length
,
5536 return MEMCACHED_SUCCESS
;
5539 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5541 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5542 test_true(rc
== MEMCACHED_SUCCESS
);
5544 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5545 test_true(rc
== MEMCACHED_SUCCESS
);
5547 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5548 test_true(rc
== MEMCACHED_SUCCESS
);
5550 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5551 test_true(rc
== MEMCACHED_SUCCESS
);
5553 return TEST_SUCCESS
;
5557 * This test ensures that the failure counter isn't incremented during
5558 * normal termination of the memcached instance.
5560 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5562 memcached_return_t rc
;
5563 memcached_server_instance_st instance
;
5565 /* Set value to force connection to the server */
5566 const char *key
= "marmotte";
5567 const char *value
= "milka";
5570 * Please note that I'm abusing the internal structures in libmemcached
5571 * in a non-portable way and you shouldn't be doing this. I'm only
5572 * doing this in order to verify that the library works the way it should
5574 uint32_t number_of_hosts
= memcached_server_count(memc
);
5575 memc
->number_of_hosts
= 1;
5577 /* Ensure that we are connected to the server by setting a value */
5578 rc
= memcached_set(memc
, key
, strlen(key
),
5579 value
, strlen(value
),
5580 (time_t)0, (uint32_t)0);
5581 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5584 instance
= memcached_server_instance_by_position(memc
, 0);
5585 /* The test is to see that the memcached_quit doesn't increase the
5586 * the server failure conter, so let's ensure that it is zero
5587 * before sending quit
5589 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5591 memcached_quit(memc
);
5593 /* Verify that it memcached_quit didn't increment the failure counter
5594 * Please note that this isn't bullet proof, because an error could
5597 test_true(instance
->server_failure_counter
== 0);
5599 /* restore the instance */
5600 memc
->number_of_hosts
= number_of_hosts
;
5602 return TEST_SUCCESS
;
5606 * This tests ensures expected disconnections (for some behavior changes
5607 * for instance) do not wrongly increase failure counter
5609 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5611 memcached_return rc
;
5613 memcached_st
*memc_clone
;
5614 memc_clone
= memcached_clone(NULL
, memc
);
5615 test_true(memc_clone
);
5617 /* Set value to force connection to the server */
5618 const char *key
= "marmotte";
5619 const char *value
= "milka";
5620 char *string
= NULL
;
5621 size_t string_length
;
5624 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5625 value
, strlen(value
),
5626 (time_t)0, (uint32_t)0);
5627 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5630 /* put failure limit to 1 */
5631 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5632 assert(rc
== MEMCACHED_SUCCESS
);
5634 /* Put a retry timeout to effectively activate failure_limit effect */
5635 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5636 assert(rc
== MEMCACHED_SUCCESS
);
5638 /* change behavior that triggers memcached_quit()*/
5639 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5640 assert(rc
== MEMCACHED_SUCCESS
);
5643 /* Check if we still are connected */
5644 string
= memcached_get(memc_clone
, key
, strlen(key
),
5645 &string_length
, &flags
, &rc
);
5647 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5650 memcached_free(memc_clone
);
5652 return TEST_SUCCESS
;
5659 * Test that ensures mget_execute does not end into recursive calls that finally fails
5661 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5663 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5664 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5665 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5666 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5667 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5670 return TEST_SKIPPED
; // My MAC can't handle this test
5674 * I only want to hit _one_ server so I know the number of requests I'm
5675 * sending in the pipeline.
5677 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5678 memc
->number_of_hosts
= 1;
5679 size_t max_keys
= 20480;
5682 char **keys
= calloc(max_keys
, sizeof(char*));
5683 size_t *key_length
=calloc(max_keys
, sizeof(size_t));
5685 /* First add all of the items.. */
5687 char blob
[1024]= { 0 };
5688 memcached_return rc
;
5689 for (size_t x
= 0; x
< max_keys
; ++x
)
5692 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5694 assert(keys
[x
] != NULL
);
5695 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5697 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5699 break; // We are out of business
5702 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5704 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5707 sleep(1);// We will try to sleep
5710 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5712 // We failed to send everything.
5717 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5720 /* Try to get all of them with a large multiget */
5722 memcached_execute_function callbacks
[1]= { [0]= &callback_counter
};
5723 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5724 (size_t)max_keys
, callbacks
, &counter
, 1);
5726 assert(rc
== MEMCACHED_SUCCESS
);
5727 char* the_value
= NULL
;
5728 char the_key
[MEMCACHED_MAX_KEY
];
5729 size_t the_key_length
;
5730 size_t the_value_length
;
5734 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5736 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5742 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5745 assert(rc
== MEMCACHED_END
);
5747 /* Verify that we got all of the items */
5748 assert(counter
== max_keys
);
5751 /* Release all allocated resources */
5752 for (size_t x
= 0; x
< max_keys
; ++x
)
5759 memc
->number_of_hosts
= number_of_hosts
;
5761 return TEST_SUCCESS
;
5764 static test_return_t
regression_bug_583031(memcached_st
*unused
)
5768 memcached_st
*memc
= memcached_create(NULL
);
5770 memcached_server_add(memc
, "10.2.3.4", 11211);
5772 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5773 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5774 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5775 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5776 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5777 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5779 memcached_return_t rc
;
5783 (void)memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5785 test_true_got(rc
== MEMCACHED_TIMEOUT
|| rc
== MEMCACHED_ERRNO
|| rc
== MEMCACHED_FAILURE
, memcached_strerror(memc
, rc
));
5787 memcached_free(memc
);
5789 return TEST_SUCCESS
;
5792 static test_return_t
regression_bug_581030(memcached_st
*unused
)
5797 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5798 test_false(local_stat
);
5800 memcached_stat_free(NULL
, NULL
);
5803 return TEST_SUCCESS
;
5806 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5808 fprintf(stderr
, "Iteration #%u: ", it
);
5810 if(error
== MEMCACHED_ERRNO
)
5812 fprintf(stderr
, "system error %d from %s: %s\n",
5813 errno
, what
, strerror(errno
));
5817 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5818 memcached_strerror(mc
, error
));
5822 #define TEST_CONSTANT_CREATION 200
5824 static test_return_t
regression_bug_(memcached_st
*memc
)
5826 const char *remote_server
;
5829 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5831 return TEST_SKIPPED
;
5834 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5836 memcached_st
* mc
= memcached_create(NULL
);
5837 memcached_return rc
;
5839 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5840 if (rc
!= MEMCACHED_SUCCESS
)
5842 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5845 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5846 if (rc
!= MEMCACHED_SUCCESS
)
5848 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5851 rc
= memcached_server_add(mc
, remote_server
, 0);
5852 if (rc
!= MEMCACHED_SUCCESS
)
5854 memcached_die(mc
, rc
, "memcached_server_add", x
);
5857 const char *set_key
= "akey";
5858 const size_t set_key_len
= strlen(set_key
);
5859 const char *set_value
= "a value";
5860 const size_t set_value_len
= strlen(set_value
);
5862 if (rc
== MEMCACHED_SUCCESS
)
5866 size_t get_value_len
;
5868 uint32_t get_value_flags
;
5870 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5871 &get_value_flags
, &rc
);
5872 if (rc
!= MEMCACHED_SUCCESS
)
5874 memcached_die(mc
, rc
, "memcached_get", x
);
5880 (get_value_len
!= set_value_len
5881 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5883 fprintf(stderr
, "Values don't match?\n");
5884 rc
= MEMCACHED_FAILURE
;
5890 rc
= memcached_set(mc
,
5891 set_key
, set_key_len
,
5892 set_value
, set_value_len
,
5896 if (rc
!= MEMCACHED_SUCCESS
)
5898 memcached_die(mc
, rc
, "memcached_set", x
);
5905 if (rc
!= MEMCACHED_SUCCESS
)
5911 return TEST_SUCCESS
;
5915 * Test that the sasl authentication works. We cannot use the default
5916 * pool of servers, because that would require that all servers we want
5917 * to test supports SASL authentication, and that they use the default
5920 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5922 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5923 memcached_return_t rc
;
5925 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5926 test_true(rc
== MEMCACHED_SUCCESS
);
5927 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5928 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5929 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5930 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5931 memcached_quit(memc
);
5933 rc
= memcached_set_sasl_auth_data(memc
,
5934 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5935 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5936 test_true(rc
== MEMCACHED_SUCCESS
);
5938 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5939 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5940 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5942 memcached_quit(memc
);
5943 return TEST_SUCCESS
;
5946 return TEST_FAILURE
;
5950 /* Clean the server before beginning testing */
5952 {"util_version", 1, (test_callback_fn
)util_version_test
},
5953 {"flush", 0, (test_callback_fn
)flush_test
},
5954 {"init", 0, (test_callback_fn
)init_test
},
5955 {"allocation", 0, (test_callback_fn
)allocation_test
},
5956 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5957 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5958 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5959 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5960 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5961 {"clone_test", 0, (test_callback_fn
)clone_test
},
5962 {"connection_test", 0, (test_callback_fn
)connection_test
},
5963 {"callback_test", 0, (test_callback_fn
)callback_test
},
5964 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5965 {"error", 0, (test_callback_fn
)error_test
},
5966 {"set", 0, (test_callback_fn
)set_test
},
5967 {"set2", 0, (test_callback_fn
)set_test2
},
5968 {"set3", 0, (test_callback_fn
)set_test3
},
5969 {"dump", 1, (test_callback_fn
)dump_test
},
5970 {"add", 1, (test_callback_fn
)add_test
},
5971 {"replace", 1, (test_callback_fn
)replace_test
},
5972 {"delete", 1, (test_callback_fn
)delete_test
},
5973 {"get", 1, (test_callback_fn
)get_test
},
5974 {"get2", 0, (test_callback_fn
)get_test2
},
5975 {"get3", 0, (test_callback_fn
)get_test3
},
5976 {"get4", 0, (test_callback_fn
)get_test4
},
5977 {"partial mget", 0, (test_callback_fn
)get_test5
},
5978 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5979 {"increment", 0, (test_callback_fn
)increment_test
},
5980 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5981 {"decrement", 0, (test_callback_fn
)decrement_test
},
5982 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5983 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5984 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5985 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5986 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5987 {"quit", 0, (test_callback_fn
)quit_test
},
5988 {"mget", 1, (test_callback_fn
)mget_test
},
5989 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5990 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5991 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5992 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5993 {"mget_end", 0, (test_callback_fn
)mget_end
},
5994 {"get_stats", 0, (test_callback_fn
)get_stats
},
5995 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5996 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5997 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5998 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5999 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
6000 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
6001 {"read_through", 1, (test_callback_fn
)read_through
},
6002 {"delete_through", 1, (test_callback_fn
)delete_through
},
6003 {"noreply", 1, (test_callback_fn
)noreply_test
},
6004 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
6005 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
6006 {"memcached_pool_test", 1, (test_callback_fn
)memcached_pool_test
},
6007 {"ping", 1, (test_callback_fn
)ping_test
},
6008 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
6009 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
6010 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
6011 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
6012 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
6016 test_st behavior_tests
[] ={
6017 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
6018 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
6019 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
6020 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
6024 test_st basic_tests
[] ={
6025 {"init", 1, (test_callback_fn
)basic_init_test
},
6026 {"clone", 1, (test_callback_fn
)basic_clone_test
},
6027 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
6028 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
6029 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
6030 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
6034 test_st regression_binary_vs_block
[] ={
6035 {"block add", 1, (test_callback_fn
)block_add_regression
},
6036 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
6040 test_st async_tests
[] ={
6041 {"add", 1, (test_callback_fn
)add_wrapper
},
6045 test_st string_tests
[] ={
6046 {"string static with null", 0, (test_callback_fn
)string_static_null
},
6047 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
6048 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
6049 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
6050 {"string append", 0, (test_callback_fn
)string_alloc_append
},
6051 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
6052 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
6053 {0, 0, (test_callback_fn
)0}
6056 test_st result_tests
[] ={
6057 {"result static", 0, (test_callback_fn
)result_static
},
6058 {"result alloc", 0, (test_callback_fn
)result_alloc
},
6059 {0, 0, (test_callback_fn
)0}
6062 test_st version_1_2_3
[] ={
6063 {"append", 0, (test_callback_fn
)append_test
},
6064 {"prepend", 0, (test_callback_fn
)prepend_test
},
6065 {"cas", 0, (test_callback_fn
)cas_test
},
6066 {"cas2", 0, (test_callback_fn
)cas2_test
},
6067 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6068 {0, 0, (test_callback_fn
)0}
6071 test_st user_tests
[] ={
6072 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6073 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6074 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6075 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6076 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6077 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6078 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6079 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6080 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6081 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6082 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6083 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6084 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6085 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6086 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6087 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6088 #if !defined(__sun) && !defined(__OpenBSD__)
6090 ** It seems to be something weird with the character sets..
6091 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6092 ** guess I need to find out how this is supposed to work.. Perhaps I need
6093 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6094 ** so just disable the code for now...).
6096 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6098 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6099 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6100 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6101 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6102 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6103 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6104 {0, 0, (test_callback_fn
)0}
6107 test_st replication_tests
[]= {
6108 {"set", 1, (test_callback_fn
)replication_set_test
},
6109 {"get", 0, (test_callback_fn
)replication_get_test
},
6110 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6111 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6112 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6113 {0, 0, (test_callback_fn
)0}
6117 * The following test suite is used to verify that we don't introduce
6118 * regression bugs. If you want more information about the bug / test,
6119 * you should look in the bug report at
6120 * http://bugs.launchpad.net/libmemcached
6122 test_st regression_tests
[]= {
6123 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6124 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6125 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6126 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6127 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6128 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6129 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6130 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6131 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6132 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6133 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6134 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6135 {0, 0, (test_callback_fn
)0}
6138 test_st sasl_auth_tests
[]= {
6139 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6140 {0, 0, (test_callback_fn
)0}
6143 test_st ketama_compatibility
[]= {
6144 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6145 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6146 {0, 0, (test_callback_fn
)0}
6149 test_st generate_tests
[] ={
6150 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6151 {"generate_data", 1, (test_callback_fn
)generate_data
},
6152 {"get_read", 0, (test_callback_fn
)get_read
},
6153 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6154 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6155 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6156 {"generate_data", 1, (test_callback_fn
)generate_data
},
6157 {"mget_read", 0, (test_callback_fn
)mget_read
},
6158 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6159 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6160 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6161 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6162 {"generate_data", 1, (test_callback_fn
)generate_data
},
6163 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6164 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6165 {0, 0, (test_callback_fn
)0}
6168 test_st consistent_tests
[] ={
6169 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6170 {"generate_data", 1, (test_callback_fn
)generate_data
},
6171 {"get_read", 0, (test_callback_fn
)get_read_count
},
6172 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6173 {0, 0, (test_callback_fn
)0}
6176 test_st consistent_weighted_tests
[] ={
6177 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6178 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6179 {"get_read", 0, (test_callback_fn
)get_read_count
},
6180 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6181 {0, 0, (test_callback_fn
)0}
6184 test_st hsieh_availability
[] ={
6185 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6186 {0, 0, (test_callback_fn
)0}
6189 test_st murmur_availability
[] ={
6190 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6191 {0, 0, (test_callback_fn
)0}
6195 test_st hash_sanity
[] ={
6196 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6197 {0, 0, (test_callback_fn
)0}
6201 test_st ketama_auto_eject_hosts
[] ={
6202 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6203 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6204 {0, 0, (test_callback_fn
)0}
6207 test_st hash_tests
[] ={
6208 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6209 {"md5", 0, (test_callback_fn
)md5_run
},
6210 {"crc", 0, (test_callback_fn
)crc_run
},
6211 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6212 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6213 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6214 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6215 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6216 {"murmur", 0, (test_callback_fn
)murmur_run
},
6217 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6218 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6219 {0, 0, (test_callback_fn
)0}
6222 test_st error_conditions
[] ={
6223 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6224 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6225 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6226 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6227 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6228 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6229 {0, 0, (test_callback_fn
)0}
6233 test_st parser_tests
[] ={
6234 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6235 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6236 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6237 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6238 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6239 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6240 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6241 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6242 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6243 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6244 {"server", 0, (test_callback_fn
)server_test
},
6245 {"bad server strings", 0, (test_callback_fn
)servers_bad_test
},
6246 {"server with weights", 0, (test_callback_fn
)server_with_weight_test
},
6247 {0, 0, (test_callback_fn
)0}
6250 test_st virtual_bucket_tests
[] ={
6251 {"basic", 0, (test_callback_fn
)virtual_back_map
},
6252 {0, 0, (test_callback_fn
)0}
6255 collection_st collection
[] ={
6257 {"hash_sanity", 0, 0, hash_sanity
},
6259 {"basic", 0, 0, basic_tests
},
6260 {"hsieh_availability", 0, 0, hsieh_availability
},
6261 {"murmur_availability", 0, 0, murmur_availability
},
6262 {"block", 0, 0, tests
},
6263 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6264 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6265 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6266 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6267 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6268 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6269 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6270 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6271 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6272 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6273 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6274 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6275 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6276 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6277 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6278 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6279 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6280 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6281 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6282 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6283 #ifdef MEMCACHED_ENABLE_DEPRECATED
6284 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6286 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6287 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6288 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6289 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6290 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6291 {"string", 0, 0, string_tests
},
6292 {"result", 0, 0, result_tests
},
6293 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6294 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6295 {"user", 0, 0, user_tests
},
6296 {"generate", 0, 0, generate_tests
},
6297 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6298 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6299 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6300 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6301 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6302 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6303 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6305 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6306 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6307 {"consistent_not", 0, 0, consistent_tests
},
6308 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6309 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6310 {"ketama_compat", 0, 0, ketama_compatibility
},
6311 {"test_hashes", 0, 0, hash_tests
},
6312 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6313 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6314 {"regression", 0, 0, regression_tests
},
6315 {"behaviors", 0, 0, behavior_tests
},
6316 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6317 {"error_conditions", 0, 0, error_conditions
},
6318 {"parser", 0, 0, parser_tests
},
6319 {"virtual buckets", 0, 0, virtual_bucket_tests
},
6323 #include "tests/libmemcached_world.h"
6325 void get_world(world_st
*world
)
6327 world
->collections
= collection
;
6329 world
->create
= (test_callback_create_fn
)world_create
;
6330 world
->destroy
= (test_callback_fn
)world_destroy
;
6332 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6333 world
->test
.flush
= (test_callback_fn
)world_flush
;
6334 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6335 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6336 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6338 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6339 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6341 world
->runner
= &defualt_libmemcached_runner
;