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.
43 #define BUILDING_LIBMEMCACHED
44 // !NEVER use common.h, always use memcached.h in your own apps
45 #include <libmemcached/common.h>
54 #include <sys/types.h>
57 #include <libtest/server.h>
59 #include "clients/generator.h"
60 #include "clients/execute.h"
62 #define SMALL_STRING_LEN 1024
64 #include <libtest/test.h>
65 #include "tests/deprecated.h"
66 #include "tests/parser.h"
67 #include "tests/pool.h"
68 #include "tests/string.h"
69 #include "tests/replication.h"
70 #include "tests/basic.h"
71 #include "tests/error_conditions.h"
72 #include "tests/print.h"
73 #include "tests/virtual_buckets.h"
76 #ifdef HAVE_LIBMEMCACHEDUTIL
78 #include "libmemcached/memcached_util.h"
81 #include "hash_results.h"
83 #define GLOBAL_COUNT 10000
84 #define GLOBAL2_COUNT 100
85 #define SERVERS_TO_CREATE 5
86 static uint32_t global_count
;
88 static pairs_st
*global_pairs
;
89 static const char *global_keys
[GLOBAL_COUNT
];
90 static size_t global_keys_length
[GLOBAL_COUNT
];
93 static test_return_t
pre_binary(memcached_st
*memc
);
96 static test_return_t
init_test(memcached_st
*not_used
)
101 (void)memcached_create(&memc
);
102 memcached_free(&memc
);
107 #define TEST_PORT_COUNT 7
108 in_port_t test_ports
[TEST_PORT_COUNT
];
110 static memcached_return_t
server_display_function(const memcached_st
*ptr
,
111 const memcached_server_st
*server
,
115 size_t bigger
= *((size_t *)(context
));
117 assert(bigger
<= memcached_server_port(server
));
118 *((size_t *)(context
))= memcached_server_port(server
);
120 return MEMCACHED_SUCCESS
;
123 static memcached_return_t
dump_server_information(const memcached_st
*ptr
,
124 const memcached_server_st
*instance
,
128 FILE *stream
= (FILE *)context
;
131 fprintf(stream
, "Memcached Server: %s %u Version %u.%u.%u\n",
132 memcached_server_name(instance
),
133 memcached_server_port(instance
),
134 instance
->major_version
,
135 instance
->minor_version
,
136 instance
->micro_version
);
138 return MEMCACHED_SUCCESS
;
141 static test_return_t
server_sort_test(memcached_st
*ptr
)
143 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
145 memcached_return_t rc
;
146 memcached_server_fn callbacks
[1];
147 memcached_st
*local_memc
;
150 local_memc
= memcached_create(NULL
);
151 test_true(local_memc
);
152 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
154 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
156 test_ports
[x
]= (in_port_t
)random() % 64000;
157 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
158 test_true(memcached_server_count(local_memc
) == x
+ 1);
160 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
162 test_true(rc
== MEMCACHED_SUCCESS
);
165 callbacks
[0]= server_display_function
;
166 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
169 memcached_free(local_memc
);
174 static test_return_t
server_sort2_test(memcached_st
*ptr
)
176 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
177 memcached_return_t rc
;
178 memcached_server_fn callbacks
[1];
179 memcached_st
*local_memc
;
180 memcached_server_instance_st instance
;
183 local_memc
= memcached_create(NULL
);
184 test_true(local_memc
);
185 rc
= memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
186 test_true(rc
== MEMCACHED_SUCCESS
);
188 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
189 test_true(rc
== MEMCACHED_SUCCESS
);
190 instance
= memcached_server_instance_by_position(local_memc
, 0);
191 test_true(memcached_server_port(instance
) == 43043);
193 rc
= memcached_server_add_with_weight(local_memc
, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
194 test_true(rc
== MEMCACHED_SUCCESS
);
196 instance
= memcached_server_instance_by_position(local_memc
, 0);
197 test_true(memcached_server_port(instance
) == 43042);
199 instance
= memcached_server_instance_by_position(local_memc
, 1);
200 test_true(memcached_server_port(instance
) == 43043);
202 callbacks
[0]= server_display_function
;
203 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
206 memcached_free(local_memc
);
211 static test_return_t
memcached_server_remove_test(memcached_st
*ptr
)
213 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";
216 memcached_st
*memc
= memcached(server_string
, strlen(server_string
));
219 memcached_server_fn callbacks
[1];
220 callbacks
[0]= server_print_callback
;
221 memcached_server_cursor(memc
, callbacks
, NULL
, 1);
223 memcached_free(memc
);
228 static memcached_return_t
server_display_unsort_function(const memcached_st
*ptr
,
229 const memcached_server_st
*server
,
233 uint32_t x
= *((uint32_t *)(context
));
236 if (! (test_ports
[x
] == server
->port
))
238 fprintf(stderr
, "%lu -> %lu\n", (unsigned long)test_ports
[x
], (unsigned long)server
->port
);
239 return MEMCACHED_FAILURE
;
242 *((uint32_t *)(context
))= ++x
;
244 return MEMCACHED_SUCCESS
;
247 static test_return_t
server_unsort_test(memcached_st
*ptr
)
249 size_t counter
= 0; /* Prime the value for the test_true in server_display_function */
250 size_t bigger
= 0; /* Prime the value for the test_true in server_display_function */
251 memcached_return_t rc
;
252 memcached_server_fn callbacks
[1];
253 memcached_st
*local_memc
;
256 local_memc
= memcached_create(NULL
);
257 test_true(local_memc
);
259 for (size_t x
= 0; x
< TEST_PORT_COUNT
; x
++)
261 test_ports
[x
]= (in_port_t
)(random() % 64000);
262 rc
= memcached_server_add_with_weight(local_memc
, "localhost", test_ports
[x
], 0);
263 test_true(memcached_server_count(local_memc
) == x
+1);
265 test_true(memcached_server_list_count(memcached_server_list(local_memc
)) == x
+1);
267 test_true(rc
== MEMCACHED_SUCCESS
);
270 callbacks
[0]= server_display_unsort_function
;
271 memcached_server_cursor(local_memc
, callbacks
, (void *)&counter
, 1);
273 /* Now we sort old data! */
274 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SORT_HOSTS
, 1);
275 callbacks
[0]= server_display_function
;
276 memcached_server_cursor(local_memc
, callbacks
, (void *)&bigger
, 1);
279 memcached_free(local_memc
);
284 static test_return_t
allocation_test(memcached_st
*not_used
)
288 memc
= memcached_create(NULL
);
290 memcached_free(memc
);
295 static test_return_t
clone_test(memcached_st
*memc
)
299 memcached_st
*memc_clone
;
300 memc_clone
= memcached_clone(NULL
, NULL
);
301 test_true(memc_clone
);
302 memcached_free(memc_clone
);
305 /* Can we init from null? */
307 memcached_st
*memc_clone
;
308 memc_clone
= memcached_clone(NULL
, memc
);
309 test_true(memc_clone
);
312 test_true(memc_clone
->allocators
.free
== memc
->allocators
.free
);
313 test_true(memc_clone
->allocators
.malloc
== memc
->allocators
.malloc
);
314 test_true(memc_clone
->allocators
.realloc
== memc
->allocators
.realloc
);
315 test_true(memc_clone
->allocators
.calloc
== memc
->allocators
.calloc
);
318 test_true(memc_clone
->connect_timeout
== memc
->connect_timeout
);
319 test_true(memc_clone
->delete_trigger
== memc
->delete_trigger
);
320 test_true(memc_clone
->distribution
== memc
->distribution
);
321 { // Test all of the flags
322 test_true(memc_clone
->flags
.no_block
== memc
->flags
.no_block
);
323 test_true(memc_clone
->flags
.tcp_nodelay
== memc
->flags
.tcp_nodelay
);
324 test_true(memc_clone
->flags
.support_cas
== memc
->flags
.support_cas
);
325 test_true(memc_clone
->flags
.buffer_requests
== memc
->flags
.buffer_requests
);
326 test_true(memc_clone
->flags
.use_sort_hosts
== memc
->flags
.use_sort_hosts
);
327 test_true(memc_clone
->flags
.verify_key
== memc
->flags
.verify_key
);
328 test_true(memc_clone
->ketama
.weighted
== memc
->ketama
.weighted
);
329 test_true(memc_clone
->flags
.binary_protocol
== memc
->flags
.binary_protocol
);
330 test_true(memc_clone
->flags
.hash_with_prefix_key
== memc
->flags
.hash_with_prefix_key
);
331 test_true(memc_clone
->flags
.no_reply
== memc
->flags
.no_reply
);
332 test_true(memc_clone
->flags
.use_udp
== memc
->flags
.use_udp
);
333 test_true(memc_clone
->flags
.auto_eject_hosts
== memc
->flags
.auto_eject_hosts
);
334 test_true(memc_clone
->flags
.randomize_replica_read
== memc
->flags
.randomize_replica_read
);
336 test_true(memc_clone
->get_key_failure
== memc
->get_key_failure
);
337 test_true(hashkit_compare(&memc_clone
->hashkit
, &memc
->hashkit
));
338 test_true(memc_clone
->io_bytes_watermark
== memc
->io_bytes_watermark
);
339 test_true(memc_clone
->io_msg_watermark
== memc
->io_msg_watermark
);
340 test_true(memc_clone
->io_key_prefetch
== memc
->io_key_prefetch
);
341 test_true(memc_clone
->on_cleanup
== memc
->on_cleanup
);
342 test_true(memc_clone
->on_clone
== memc
->on_clone
);
343 test_true(memc_clone
->poll_timeout
== memc
->poll_timeout
);
344 test_true(memc_clone
->rcv_timeout
== memc
->rcv_timeout
);
345 test_true(memc_clone
->recv_size
== memc
->recv_size
);
346 test_true(memc_clone
->retry_timeout
== memc
->retry_timeout
);
347 test_true(memc_clone
->send_size
== memc
->send_size
);
348 test_true(memc_clone
->server_failure_limit
== memc
->server_failure_limit
);
349 test_true(memc_clone
->snd_timeout
== memc
->snd_timeout
);
350 test_true(memc_clone
->user_data
== memc
->user_data
);
352 memcached_free(memc_clone
);
355 /* Can we init from struct? */
357 memcached_st declared_clone
;
358 memcached_st
*memc_clone
;
359 memset(&declared_clone
, 0 , sizeof(memcached_st
));
360 memc_clone
= memcached_clone(&declared_clone
, NULL
);
361 test_true(memc_clone
);
362 memcached_free(memc_clone
);
365 /* Can we init from struct? */
367 memcached_st declared_clone
;
368 memcached_st
*memc_clone
;
369 memset(&declared_clone
, 0 , sizeof(memcached_st
));
370 memc_clone
= memcached_clone(&declared_clone
, memc
);
371 test_true(memc_clone
);
372 memcached_free(memc_clone
);
378 static test_return_t
userdata_test(memcached_st
*memc
)
381 test_true(memcached_set_user_data(memc
, foo
) == NULL
);
382 test_true(memcached_get_user_data(memc
) == foo
);
383 test_true(memcached_set_user_data(memc
, NULL
) == foo
);
388 static test_return_t
connection_test(memcached_st
*memc
)
390 memcached_return_t rc
;
392 rc
= memcached_server_add_with_weight(memc
, "localhost", 0, 0);
393 test_true(rc
== MEMCACHED_SUCCESS
);
398 static test_return_t
error_test(memcached_st
*memc
)
400 uint32_t values
[] = { 851992627U, 2337886783U, 646418395U, 4001849190U,
401 982370485U, 1263635348U, 4242906218U, 3829656100U,
402 1891735253U, 334139633U, 2257084983U, 3088286104U,
403 13199785U, 2542027183U, 1097051614U, 199566778U,
404 2748246961U, 2465192557U, 1664094137U, 2405439045U,
405 1842224848U, 692413798U, 3479807801U, 919913813U,
406 4269430871U, 610793021U, 527273862U, 1437122909U,
407 2300930706U, 2943759320U, 674306647U, 2400528935U,
408 54481931U, 4186304426U, 1741088401U, 2979625118U,
409 4159057246U, 3425930182U, 2593724503U, 1868899624U,
410 1769812374U, 2302537950U, 1110330676U, 3365377466U,
411 1336171666U, 3021258493U, 3365377466U };
413 // You have updated the memcache_error messages but not updated docs/tests.
414 for (int rc
= int(MEMCACHED_SUCCESS
); rc
< int(MEMCACHED_MAXIMUM_RETURN
); ++rc
)
417 const char *msg
= memcached_strerror(memc
, memcached_return_t(rc
));
418 hash_val
= memcached_generate_hash_value(msg
, strlen(msg
),
419 MEMCACHED_HASH_JENKINS
);
420 if (values
[rc
] != hash_val
)
422 fprintf(stderr
, "\n\nYou have updated memcached_return_t without updating the error_test\n");
423 fprintf(stderr
, "%u, %s, (%u)\n\n", (uint32_t)rc
, memcached_strerror(memc
, memcached_return_t(rc
)), hash_val
);
425 test_true(values
[rc
] == hash_val
);
427 test_true(MEMCACHED_MAXIMUM_RETURN
== 46);
432 static test_return_t
set_test(memcached_st
*memc
)
434 memcached_return_t rc
= memcached_set(memc
,
435 memcached_literal_param("foo"),
436 memcached_literal_param("when we sanitize"),
437 time_t(0), (uint32_t)0);
438 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
443 static test_return_t
append_test(memcached_st
*memc
)
445 memcached_return_t rc
;
446 const char *key
= "fig";
447 const char *in_value
= "we";
448 char *out_value
= NULL
;
452 rc
= memcached_flush(memc
, 0);
453 test_true(rc
== MEMCACHED_SUCCESS
);
455 rc
= memcached_set(memc
, key
, strlen(key
),
456 in_value
, strlen(in_value
),
457 (time_t)0, (uint32_t)0);
458 test_true(rc
== MEMCACHED_SUCCESS
);
460 rc
= memcached_append(memc
, key
, strlen(key
),
461 " the", strlen(" the"),
462 (time_t)0, (uint32_t)0);
463 test_true(rc
== MEMCACHED_SUCCESS
);
465 rc
= memcached_append(memc
, key
, strlen(key
),
466 " people", strlen(" people"),
467 (time_t)0, (uint32_t)0);
468 test_true(rc
== MEMCACHED_SUCCESS
);
470 out_value
= memcached_get(memc
, key
, strlen(key
),
471 &value_length
, &flags
, &rc
);
472 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
473 test_true(strlen("we the people") == value_length
);
474 test_true(rc
== MEMCACHED_SUCCESS
);
480 static test_return_t
append_binary_test(memcached_st
*memc
)
482 memcached_return_t rc
;
483 const char *key
= "numbers";
484 uint32_t store_list
[] = { 23, 56, 499, 98, 32847, 0 };
490 rc
= memcached_flush(memc
, 0);
491 test_true(rc
== MEMCACHED_SUCCESS
);
493 rc
= memcached_set(memc
,
496 (time_t)0, (uint32_t)0);
497 test_true(rc
== MEMCACHED_SUCCESS
);
499 for (x
= 0; store_list
[x
] ; x
++)
501 rc
= memcached_append(memc
,
503 (char *)&store_list
[x
], sizeof(uint32_t),
504 (time_t)0, (uint32_t)0);
505 test_true(rc
== MEMCACHED_SUCCESS
);
508 value
= (uint32_t *)memcached_get(memc
, key
, strlen(key
),
509 &value_length
, &flags
, &rc
);
510 test_true((value_length
== (sizeof(uint32_t) * x
)));
511 test_true(rc
== MEMCACHED_SUCCESS
);
513 for (uint32_t counter
= x
, *ptr
= value
; counter
; counter
--)
515 test_true(*ptr
== store_list
[x
- counter
]);
523 static test_return_t
cas2_test(memcached_st
*memc
)
525 memcached_return_t rc
;
526 const char *keys
[]= {"fudge", "son", "food"};
527 size_t key_length
[]= {5, 3, 4};
528 const char *value
= "we the people";
529 size_t value_length
= strlen("we the people");
531 memcached_result_st results_obj
;
532 memcached_result_st
*results
;
535 rc
= memcached_flush(memc
, 0);
536 test_true(rc
== MEMCACHED_SUCCESS
);
538 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
540 for (x
= 0; x
< 3; x
++)
542 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
543 keys
[x
], key_length
[x
],
544 (time_t)50, (uint32_t)9);
545 test_true(rc
== MEMCACHED_SUCCESS
);
548 rc
= memcached_mget(memc
, keys
, key_length
, 3);
550 results
= memcached_result_create(memc
, &results_obj
);
552 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
554 test_true(results
->item_cas
);
555 test_true(rc
== MEMCACHED_SUCCESS
);
556 test_true(memcached_result_cas(results
));
558 test_true(!memcmp(value
, "we the people", strlen("we the people")));
559 test_true(strlen("we the people") == value_length
);
560 test_true(rc
== MEMCACHED_SUCCESS
);
562 memcached_result_free(&results_obj
);
567 static test_return_t
cas_test(memcached_st
*memc
)
569 memcached_return_t rc
;
570 const char *key
= "fun";
571 size_t key_length
= strlen(key
);
572 const char *value
= "we the people";
573 const char* keys
[2] = { key
, NULL
};
574 size_t keylengths
[2] = { strlen(key
), 0 };
575 size_t value_length
= strlen(value
);
576 const char *value2
= "change the value";
577 size_t value2_length
= strlen(value2
);
579 memcached_result_st results_obj
;
580 memcached_result_st
*results
;
583 rc
= memcached_flush(memc
, 0);
584 test_true(rc
== MEMCACHED_SUCCESS
);
586 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
588 rc
= memcached_set(memc
, key
, strlen(key
),
589 value
, strlen(value
),
590 (time_t)0, (uint32_t)0);
591 test_true(rc
== MEMCACHED_SUCCESS
);
593 rc
= memcached_mget(memc
, keys
, keylengths
, 1);
595 results
= memcached_result_create(memc
, &results_obj
);
597 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
599 test_true(rc
== MEMCACHED_SUCCESS
);
600 test_true(memcached_result_cas(results
));
601 test_true(!memcmp(value
, memcached_result_value(results
), value_length
));
602 test_true(strlen(memcached_result_value(results
)) == value_length
);
603 test_true(rc
== MEMCACHED_SUCCESS
);
604 uint64_t cas
= memcached_result_cas(results
);
607 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
608 test_true(rc
== MEMCACHED_END
);
609 test_true(results
== NULL
);
612 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
613 test_true(rc
== MEMCACHED_SUCCESS
);
616 * The item will have a new cas value, so try to set it again with the old
617 * value. This should fail!
619 rc
= memcached_cas(memc
, key
, key_length
, value2
, value2_length
, 0, 0, cas
);
620 test_true(rc
== MEMCACHED_DATA_EXISTS
);
622 memcached_result_free(&results_obj
);
627 static test_return_t
prepend_test(memcached_st
*memc
)
629 memcached_return_t rc
;
630 const char *key
= "fig";
631 const char *value
= "people";
632 char *out_value
= NULL
;
636 rc
= memcached_flush(memc
, 0);
637 test_true(rc
== MEMCACHED_SUCCESS
);
639 rc
= memcached_set(memc
, key
, strlen(key
),
640 value
, strlen(value
),
641 (time_t)0, (uint32_t)0);
642 test_true(rc
== MEMCACHED_SUCCESS
);
644 rc
= memcached_prepend(memc
, key
, strlen(key
),
645 "the ", strlen("the "),
646 (time_t)0, (uint32_t)0);
647 test_true(rc
== MEMCACHED_SUCCESS
);
649 rc
= memcached_prepend(memc
, key
, strlen(key
),
650 "we ", strlen("we "),
651 (time_t)0, (uint32_t)0);
652 test_true(rc
== MEMCACHED_SUCCESS
);
654 out_value
= memcached_get(memc
, key
, strlen(key
),
655 &value_length
, &flags
, &rc
);
656 test_true(!memcmp(out_value
, "we the people", strlen("we the people")));
657 test_true(strlen("we the people") == value_length
);
658 test_true(rc
== MEMCACHED_SUCCESS
);
665 Set the value, then quit to make sure it is flushed.
666 Come back in and test that add fails.
668 static test_return_t
add_test(memcached_st
*memc
)
670 memcached_return_t rc
;
671 const char *key
= "foo";
672 const char *value
= "when we sanitize";
673 unsigned long long setting_value
;
675 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
677 rc
= memcached_set(memc
, key
, strlen(key
),
678 value
, strlen(value
),
679 (time_t)0, (uint32_t)0);
680 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
681 memcached_quit(memc
);
682 rc
= memcached_add(memc
, key
, strlen(key
),
683 value
, strlen(value
),
684 (time_t)0, (uint32_t)0);
686 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
689 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
693 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_DATA_EXISTS
);
700 ** There was a problem of leaking filedescriptors in the initial release
701 ** of MacOSX 10.5. This test case triggers the problem. On some Solaris
702 ** systems it seems that the kernel is slow on reclaiming the resources
703 ** because the connects starts to time out (the test doesn't do much
704 ** anyway, so just loop 10 iterations)
706 static test_return_t
add_wrapper(memcached_st
*memc
)
708 unsigned int max
= 10000;
716 for (uint32_t x
= 0; x
< max
; x
++)
722 static test_return_t
replace_test(memcached_st
*memc
)
724 memcached_return_t rc
;
725 const char *key
= "foo";
726 const char *value
= "when we sanitize";
727 const char *original
= "first we insert some data";
729 rc
= memcached_set(memc
, key
, strlen(key
),
730 original
, strlen(original
),
731 (time_t)0, (uint32_t)0);
732 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
734 rc
= memcached_replace(memc
, key
, strlen(key
),
735 value
, strlen(value
),
736 (time_t)0, (uint32_t)0);
737 test_true(rc
== MEMCACHED_SUCCESS
);
742 static test_return_t
delete_test(memcached_st
*memc
)
744 memcached_return_t rc
;
745 const char *key
= "foo";
746 const char *value
= "when we sanitize";
748 rc
= memcached_set(memc
, key
, strlen(key
),
749 value
, strlen(value
),
750 (time_t)0, (uint32_t)0);
751 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
753 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
754 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
759 static test_return_t
flush_test(memcached_st
*memc
)
761 memcached_return_t rc
;
763 uint64_t query_id
= memcached_query_id(memc
);
764 rc
= memcached_flush(memc
, 0);
765 test_compare(rc
, MEMCACHED_SUCCESS
);
766 test_compare(query_id
+1, memcached_query_id(memc
));
771 static memcached_return_t
server_function(const memcached_st
*ptr
,
772 const memcached_server_st
*server
,
775 (void)ptr
; (void)server
; (void)context
;
778 return MEMCACHED_SUCCESS
;
781 static test_return_t
memcached_server_cursor_test(memcached_st
*memc
)
784 strncpy(context
, "foo bad", sizeof(context
));
785 memcached_server_fn callbacks
[1];
787 callbacks
[0]= server_function
;
788 memcached_server_cursor(memc
, callbacks
, context
, 1);
792 static test_return_t
bad_key_test(memcached_st
*memc
)
794 memcached_return_t rc
;
795 const char *key
= "foo bad";
797 size_t string_length
;
799 memcached_st
*memc_clone
;
801 size_t max_keylen
= 0xffff;
803 // Just skip if we are in binary mode.
804 uint64_t query_id
= memcached_query_id(memc
);
805 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
))
807 test_compare(query_id
, memcached_query_id(memc
)); // We should not increase the query_id for memcached_behavior_get()
809 memc_clone
= memcached_clone(NULL
, memc
);
810 test_true(memc_clone
);
812 query_id
= memcached_query_id(memc_clone
);
813 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
814 test_true(rc
== MEMCACHED_SUCCESS
);
815 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
817 /* All keys are valid in the binary protocol (except for length) */
818 if (memcached_behavior_get(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 0)
820 query_id
= memcached_query_id(memc_clone
);
821 string
= memcached_get(memc_clone
, key
, strlen(key
),
822 &string_length
, &flags
, &rc
);
823 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
824 test_true(string_length
== 0);
828 query_id
= memcached_query_id(memc_clone
);
829 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
830 test_compare(query_id
, memcached_query_id(memc_clone
)); // We should not increase the query_id for memcached_behavior_set()
831 test_true(rc
== MEMCACHED_SUCCESS
);
832 string
= memcached_get(memc_clone
, key
, strlen(key
),
833 &string_length
, &flags
, &rc
);
834 test_true(rc
== MEMCACHED_NOTFOUND
);
835 test_true(string_length
== 0);
838 /* Test multi key for bad keys */
839 const char *keys
[] = { "GoodKey", "Bad Key", "NotMine" };
840 size_t key_lengths
[] = { 7, 7, 7 };
842 query_id
= memcached_query_id(memc_clone
);
843 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
844 test_true(rc
== MEMCACHED_SUCCESS
);
845 test_compare(query_id
, memcached_query_id(memc_clone
));
847 query_id
= memcached_query_id(memc_clone
);
848 rc
= memcached_mget(memc_clone
, keys
, key_lengths
, 3);
849 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
850 test_compare(query_id
+1, memcached_query_id(memc_clone
));
852 query_id
= memcached_query_id(memc_clone
);
853 rc
= memcached_mget_by_key(memc_clone
, "foo daddy", 9, keys
, key_lengths
, 1);
854 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
855 test_compare(query_id
+1, memcached_query_id(memc_clone
));
859 /* The following test should be moved to the end of this function when the
860 memcached server is updated to allow max size length of the keys in the
863 rc
= memcached_callback_set(memc_clone
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
864 test_true(rc
== MEMCACHED_SUCCESS
);
866 char *longkey
= (char *)malloc(max_keylen
+ 1);
869 memset(longkey
, 'a', max_keylen
+ 1);
870 string
= memcached_get(memc_clone
, longkey
, max_keylen
,
871 &string_length
, &flags
, &rc
);
872 test_true(rc
== MEMCACHED_NOTFOUND
);
873 test_true(string_length
== 0);
876 string
= memcached_get(memc_clone
, longkey
, max_keylen
+ 1,
877 &string_length
, &flags
, &rc
);
878 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
879 test_true(string_length
== 0);
886 /* Make sure zero length keys are marked as bad */
888 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, set
);
889 test_true(rc
== MEMCACHED_SUCCESS
);
890 string
= memcached_get(memc_clone
, key
, 0,
891 &string_length
, &flags
, &rc
);
892 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
893 test_true(string_length
== 0);
896 memcached_free(memc_clone
);
901 #define READ_THROUGH_VALUE "set for me"
902 static memcached_return_t
read_through_trigger(memcached_st
*memc
,
905 memcached_result_st
*result
)
907 (void)memc
;(void)key
;(void)key_length
;
908 return memcached_result_set_value(result
, READ_THROUGH_VALUE
, strlen(READ_THROUGH_VALUE
));
911 #ifndef __INTEL_COMPILER
912 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
915 static test_return_t
read_through(memcached_st
*memc
)
917 memcached_return_t rc
;
918 const char *key
= "foo";
920 size_t string_length
;
922 memcached_trigger_key_fn cb
= (memcached_trigger_key_fn
)read_through_trigger
;
924 string
= memcached_get(memc
, key
, strlen(key
),
925 &string_length
, &flags
, &rc
);
927 test_true(rc
== MEMCACHED_NOTFOUND
);
928 test_false(string_length
);
931 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_GET_FAILURE
, *(void **)&cb
);
932 test_true(rc
== MEMCACHED_SUCCESS
);
934 string
= memcached_get(memc
, key
, strlen(key
),
935 &string_length
, &flags
, &rc
);
937 test_true(rc
== MEMCACHED_SUCCESS
);
938 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
939 test_strcmp(READ_THROUGH_VALUE
, string
);
942 string
= memcached_get(memc
, key
, strlen(key
),
943 &string_length
, &flags
, &rc
);
945 test_true(rc
== MEMCACHED_SUCCESS
);
946 test_true(string_length
== strlen(READ_THROUGH_VALUE
));
947 test_true(!strcmp(READ_THROUGH_VALUE
, string
));
953 static memcached_return_t
delete_trigger(memcached_st
*ptr
,
957 (void)ptr
;(void)key_length
;
960 return MEMCACHED_SUCCESS
;
963 static test_return_t
delete_through(memcached_st
*memc
)
965 memcached_trigger_delete_key_fn callback
;
966 memcached_return_t rc
;
968 callback
= (memcached_trigger_delete_key_fn
)delete_trigger
;
970 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_DELETE_TRIGGER
, *(void**)&callback
);
971 test_true(rc
== MEMCACHED_SUCCESS
);
976 static test_return_t
get_test(memcached_st
*memc
)
978 memcached_return_t rc
;
979 const char *key
= "foo";
981 size_t string_length
;
984 uint64_t query_id
= memcached_query_id(memc
);
985 rc
= memcached_delete(memc
, key
, strlen(key
), (time_t)0);
986 test_true(rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_NOTFOUND
);
987 test_compare(query_id
+1, memcached_query_id(memc
));
989 string
= memcached_get(memc
, key
, strlen(key
),
990 &string_length
, &flags
, &rc
);
992 test_true(rc
== MEMCACHED_NOTFOUND
);
993 test_false(string_length
);
999 static test_return_t
get_test2(memcached_st
*memc
)
1001 memcached_return_t rc
;
1002 const char *key
= "foo";
1003 const char *value
= "when we sanitize";
1005 size_t string_length
;
1008 uint64_t query_id
= memcached_query_id(memc
);
1009 rc
= memcached_set(memc
, key
, strlen(key
),
1010 value
, strlen(value
),
1011 (time_t)0, (uint32_t)0);
1012 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1013 test_compare(query_id
+1, memcached_query_id(memc
));
1015 query_id
= memcached_query_id(memc
);
1016 string
= memcached_get(memc
, key
, strlen(key
),
1017 &string_length
, &flags
, &rc
);
1018 test_compare(query_id
+1, memcached_query_id(memc
));
1021 test_true(rc
== MEMCACHED_SUCCESS
);
1022 test_true(string_length
== strlen(value
));
1023 test_memcmp(string
, value
, string_length
);
1027 return TEST_SUCCESS
;
1030 static test_return_t
set_test2(memcached_st
*memc
)
1032 memcached_return_t rc
;
1033 const char *key
= "foo";
1034 const char *value
= "train in the brain";
1035 size_t value_length
= strlen(value
);
1038 for (x
= 0; x
< 10; x
++)
1040 rc
= memcached_set(memc
, key
, strlen(key
),
1041 value
, value_length
,
1042 (time_t)0, (uint32_t)0);
1043 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1046 return TEST_SUCCESS
;
1049 static test_return_t
set_test3(memcached_st
*memc
)
1051 memcached_return_t rc
;
1053 size_t value_length
= 8191;
1055 value
= (char*)malloc(value_length
);
1058 for (uint32_t x
= 0; x
< value_length
; x
++)
1059 value
[x
] = (char) (x
% 127);
1061 /* The dump test relies on there being at least 32 items in memcached */
1062 for (uint32_t x
= 0; x
< 32; x
++)
1066 snprintf(key
, sizeof(key
), "foo%u", x
);
1068 uint64_t query_id
= memcached_query_id(memc
);
1069 rc
= memcached_set(memc
, key
, strlen(key
),
1070 value
, value_length
,
1071 (time_t)0, (uint32_t)0);
1072 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1073 test_compare(query_id
+1, memcached_query_id(memc
));
1078 return TEST_SUCCESS
;
1081 static test_return_t
get_test3(memcached_st
*memc
)
1083 memcached_return_t rc
;
1084 const char *key
= "foo";
1086 size_t value_length
= 8191;
1088 size_t string_length
;
1092 value
= (char*)malloc(value_length
);
1095 for (x
= 0; x
< value_length
; x
++)
1096 value
[x
] = (char) (x
% 127);
1098 rc
= memcached_set(memc
, key
, strlen(key
),
1099 value
, value_length
,
1100 (time_t)0, (uint32_t)0);
1101 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1103 string
= memcached_get(memc
, key
, strlen(key
),
1104 &string_length
, &flags
, &rc
);
1106 test_true(rc
== MEMCACHED_SUCCESS
);
1108 test_true(string_length
== value_length
);
1109 test_true(!memcmp(string
, value
, string_length
));
1114 return TEST_SUCCESS
;
1117 static test_return_t
get_test4(memcached_st
*memc
)
1119 memcached_return_t rc
;
1120 const char *key
= "foo";
1122 size_t value_length
= 8191;
1124 size_t string_length
;
1128 value
= (char*)malloc(value_length
);
1131 for (x
= 0; x
< value_length
; x
++)
1132 value
[x
] = (char) (x
% 127);
1134 rc
= memcached_set(memc
, key
, strlen(key
),
1135 value
, value_length
,
1136 (time_t)0, (uint32_t)0);
1137 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1139 for (x
= 0; x
< 10; x
++)
1141 string
= memcached_get(memc
, key
, strlen(key
),
1142 &string_length
, &flags
, &rc
);
1144 test_true(rc
== MEMCACHED_SUCCESS
);
1146 test_true(string_length
== value_length
);
1147 test_true(!memcmp(string
, value
, string_length
));
1153 return TEST_SUCCESS
;
1157 * This test verifies that memcached_read_one_response doesn't try to
1158 * dereference a NIL-pointer if you issue a multi-get and don't read out all
1159 * responses before you execute a storage command.
1161 static test_return_t
get_test5(memcached_st
*memc
)
1164 ** Request the same key twice, to ensure that we hash to the same server
1165 ** (so that we have multiple response values queued up) ;-)
1167 const char *keys
[]= { "key", "key" };
1168 size_t lengths
[]= { 3, 3 };
1172 memcached_return_t rc
= memcached_set(memc
, keys
[0], lengths
[0],
1173 keys
[0], lengths
[0], 0, 0);
1174 test_true(rc
== MEMCACHED_SUCCESS
);
1175 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1177 memcached_result_st results_obj
;
1178 memcached_result_st
*results
;
1179 results
=memcached_result_create(memc
, &results_obj
);
1181 results
=memcached_fetch_result(memc
, &results_obj
, &rc
);
1183 memcached_result_free(&results_obj
);
1185 /* Don't read out the second result, but issue a set instead.. */
1186 rc
= memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0);
1187 test_true(rc
== MEMCACHED_SUCCESS
);
1189 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
1190 &rlen
, &flags
, &rc
);
1191 test_true(val
== NULL
);
1192 test_true(rc
== MEMCACHED_NOTFOUND
);
1193 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
1194 test_true(val
!= NULL
);
1195 test_true(rc
== MEMCACHED_SUCCESS
);
1198 return TEST_SUCCESS
;
1201 static test_return_t
mget_end(memcached_st
*memc
)
1203 const char *keys
[]= { "foo", "foo2" };
1204 size_t lengths
[]= { 3, 4 };
1205 const char *values
[]= { "fjord", "41" };
1207 memcached_return_t rc
;
1210 for (int i
= 0; i
< 2; i
++)
1212 rc
= memcached_set(memc
, keys
[i
], lengths
[i
], values
[i
], strlen(values
[i
]),
1213 (time_t)0, (uint32_t)0);
1214 test_true(rc
== MEMCACHED_SUCCESS
);
1218 size_t string_length
;
1221 // retrieve both via mget
1222 rc
= memcached_mget(memc
, keys
, lengths
, 2);
1223 test_true(rc
== MEMCACHED_SUCCESS
);
1225 char key
[MEMCACHED_MAX_KEY
];
1228 // this should get both
1229 for (int i
= 0; i
< 2; i
++)
1231 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
,
1233 test_true(rc
== MEMCACHED_SUCCESS
);
1235 if (key_length
== 4)
1237 test_true(string_length
== strlen(values
[val
]));
1238 test_true(strncmp(values
[val
], string
, string_length
) == 0);
1242 // this should indicate end
1243 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1244 test_true(rc
== MEMCACHED_END
);
1247 rc
= memcached_mget(memc
, keys
, lengths
, 1);
1248 test_true(rc
== MEMCACHED_SUCCESS
);
1250 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1251 test_true(key_length
== lengths
[0]);
1252 test_true(strncmp(keys
[0], key
, key_length
) == 0);
1253 test_true(string_length
== strlen(values
[0]));
1254 test_true(strncmp(values
[0], string
, string_length
) == 0);
1255 test_true(rc
== MEMCACHED_SUCCESS
);
1258 // this should indicate end
1259 string
= memcached_fetch(memc
, key
, &key_length
, &string_length
, &flags
, &rc
);
1260 test_true(rc
== MEMCACHED_END
);
1262 return TEST_SUCCESS
;
1265 /* Do not copy the style of this code, I just access hosts to testthis function */
1266 static test_return_t
stats_servername_test(memcached_st
*memc
)
1268 memcached_return_t rc
;
1269 memcached_stat_st memc_stat
;
1270 memcached_server_instance_st instance
=
1271 memcached_server_instance_by_position(memc
, 0);
1273 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
1274 if (memcached_get_sasl_callbacks(memc
) != NULL
)
1275 return TEST_SKIPPED
;
1277 rc
= memcached_stat_servername(&memc_stat
, NULL
,
1278 memcached_server_name(instance
),
1279 memcached_server_port(instance
));
1281 return TEST_SUCCESS
;
1284 static test_return_t
increment_test(memcached_st
*memc
)
1286 uint64_t new_number
;
1287 memcached_return_t rc
;
1288 const char *key
= "number";
1289 const char *value
= "0";
1291 rc
= memcached_set(memc
, key
, strlen(key
),
1292 value
, strlen(value
),
1293 (time_t)0, (uint32_t)0);
1294 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1296 rc
= memcached_increment(memc
, key
, strlen(key
),
1298 test_true(rc
== MEMCACHED_SUCCESS
);
1299 test_true(new_number
== 1);
1301 rc
= memcached_increment(memc
, key
, strlen(key
),
1303 test_true(rc
== MEMCACHED_SUCCESS
);
1304 test_true(new_number
== 2);
1306 return TEST_SUCCESS
;
1309 static test_return_t
increment_with_initial_test(memcached_st
*memc
)
1311 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1313 uint64_t new_number
;
1314 memcached_return_t rc
;
1315 const char *key
= "number";
1316 uint64_t initial
= 0;
1318 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1319 1, initial
, 0, &new_number
);
1320 test_true(rc
== MEMCACHED_SUCCESS
);
1321 test_true(new_number
== initial
);
1323 rc
= memcached_increment_with_initial(memc
, key
, strlen(key
),
1324 1, initial
, 0, &new_number
);
1325 test_true(rc
== MEMCACHED_SUCCESS
);
1326 test_true(new_number
== (initial
+ 1));
1328 return TEST_SUCCESS
;
1331 static test_return_t
decrement_test(memcached_st
*memc
)
1333 uint64_t new_number
;
1334 memcached_return_t rc
;
1335 const char *key
= "number";
1336 const char *value
= "3";
1338 rc
= memcached_set(memc
, key
, strlen(key
),
1339 value
, strlen(value
),
1340 (time_t)0, (uint32_t)0);
1341 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1343 rc
= memcached_decrement(memc
, key
, strlen(key
),
1345 test_true(rc
== MEMCACHED_SUCCESS
);
1346 test_true(new_number
== 2);
1348 rc
= memcached_decrement(memc
, key
, strlen(key
),
1350 test_true(rc
== MEMCACHED_SUCCESS
);
1351 test_true(new_number
== 1);
1353 return TEST_SUCCESS
;
1356 static test_return_t
decrement_with_initial_test(memcached_st
*memc
)
1358 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1360 uint64_t new_number
;
1361 memcached_return_t rc
;
1362 const char *key
= "number";
1363 uint64_t initial
= 3;
1365 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1366 1, initial
, 0, &new_number
);
1367 test_true(rc
== MEMCACHED_SUCCESS
);
1368 test_true(new_number
== initial
);
1370 rc
= memcached_decrement_with_initial(memc
, key
, strlen(key
),
1371 1, initial
, 0, &new_number
);
1372 test_true(rc
== MEMCACHED_SUCCESS
);
1373 test_true(new_number
== (initial
- 1));
1375 return TEST_SUCCESS
;
1378 static test_return_t
increment_by_key_test(memcached_st
*memc
)
1380 uint64_t new_number
;
1381 memcached_return_t rc
;
1382 const char *master_key
= "foo";
1383 const char *key
= "number";
1384 const char *value
= "0";
1386 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1388 value
, strlen(value
),
1389 (time_t)0, (uint32_t)0);
1390 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1392 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1394 test_true(rc
== MEMCACHED_SUCCESS
);
1395 test_true(new_number
== 1);
1397 rc
= memcached_increment_by_key(memc
, master_key
, strlen(master_key
), key
, strlen(key
),
1399 test_true(rc
== MEMCACHED_SUCCESS
);
1400 test_true(new_number
== 2);
1402 return TEST_SUCCESS
;
1405 static test_return_t
increment_with_initial_by_key_test(memcached_st
*memc
)
1407 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1409 uint64_t new_number
;
1410 memcached_return_t rc
;
1411 const char *master_key
= "foo";
1412 const char *key
= "number";
1413 uint64_t initial
= 0;
1415 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1417 1, initial
, 0, &new_number
);
1418 test_true(rc
== MEMCACHED_SUCCESS
);
1419 test_true(new_number
== initial
);
1421 rc
= memcached_increment_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1423 1, initial
, 0, &new_number
);
1424 test_true(rc
== MEMCACHED_SUCCESS
);
1425 test_true(new_number
== (initial
+ 1));
1427 return TEST_SUCCESS
;
1430 static test_return_t
decrement_by_key_test(memcached_st
*memc
)
1432 uint64_t new_number
;
1433 memcached_return_t rc
;
1434 const char *master_key
= "foo";
1435 const char *key
= "number";
1436 const char *value
= "3";
1438 rc
= memcached_set_by_key(memc
, master_key
, strlen(master_key
),
1440 value
, strlen(value
),
1441 (time_t)0, (uint32_t)0);
1442 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1444 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1447 test_true(rc
== MEMCACHED_SUCCESS
);
1448 test_true(new_number
== 2);
1450 rc
= memcached_decrement_by_key(memc
, master_key
, strlen(master_key
),
1453 test_true(rc
== MEMCACHED_SUCCESS
);
1454 test_true(new_number
== 1);
1456 return TEST_SUCCESS
;
1459 static test_return_t
decrement_with_initial_by_key_test(memcached_st
*memc
)
1461 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1463 uint64_t new_number
;
1464 memcached_return_t rc
;
1465 const char *master_key
= "foo";
1466 const char *key
= "number";
1467 uint64_t initial
= 3;
1469 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1471 1, initial
, 0, &new_number
);
1472 test_true(rc
== MEMCACHED_SUCCESS
);
1473 test_true(new_number
== initial
);
1475 rc
= memcached_decrement_with_initial_by_key(memc
, master_key
, strlen(master_key
),
1477 1, initial
, 0, &new_number
);
1478 test_true(rc
== MEMCACHED_SUCCESS
);
1479 test_true(new_number
== (initial
- 1));
1481 return TEST_SUCCESS
;
1484 static test_return_t
quit_test(memcached_st
*memc
)
1486 memcached_return_t rc
;
1487 const char *key
= "fudge";
1488 const char *value
= "sanford and sun";
1490 rc
= memcached_set(memc
, key
, strlen(key
),
1491 value
, strlen(value
),
1492 (time_t)10, (uint32_t)3);
1493 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1494 memcached_quit(memc
);
1496 rc
= memcached_set(memc
, key
, strlen(key
),
1497 value
, strlen(value
),
1498 (time_t)50, (uint32_t)9);
1499 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1501 return TEST_SUCCESS
;
1504 static test_return_t
mget_result_test(memcached_st
*memc
)
1506 memcached_return_t rc
;
1507 const char *keys
[]= {"fudge", "son", "food"};
1508 size_t key_length
[]= {5, 3, 4};
1511 memcached_result_st results_obj
;
1512 memcached_result_st
*results
;
1514 results
= memcached_result_create(memc
, &results_obj
);
1516 test_true(&results_obj
== results
);
1518 /* We need to empty the server before continueing test */
1519 rc
= memcached_flush(memc
, 0);
1520 test_true(rc
== MEMCACHED_SUCCESS
);
1522 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1523 test_true(rc
== MEMCACHED_SUCCESS
);
1525 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1530 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)) != NULL
)
1531 test_true(!results
);
1532 test_true(rc
== MEMCACHED_END
);
1534 for (x
= 0; x
< 3; x
++)
1536 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1537 keys
[x
], key_length
[x
],
1538 (time_t)50, (uint32_t)9);
1539 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1542 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1543 test_true(rc
== MEMCACHED_SUCCESS
);
1545 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
1548 test_true(&results_obj
== results
);
1549 test_true(rc
== MEMCACHED_SUCCESS
);
1550 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1551 test_true(!memcmp(memcached_result_key_value(results
),
1552 memcached_result_value(results
),
1553 memcached_result_length(results
)));
1556 memcached_result_free(&results_obj
);
1558 return TEST_SUCCESS
;
1561 static test_return_t
mget_result_alloc_test(memcached_st
*memc
)
1563 memcached_return_t rc
;
1564 const char *keys
[]= {"fudge", "son", "food"};
1565 size_t key_length
[]= {5, 3, 4};
1568 memcached_result_st
*results
;
1570 /* We need to empty the server before continueing test */
1571 rc
= memcached_flush(memc
, 0);
1572 test_true(rc
== MEMCACHED_SUCCESS
);
1574 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1575 test_true(rc
== MEMCACHED_SUCCESS
);
1577 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)) != NULL
)
1581 test_true(!results
);
1582 test_true(rc
== MEMCACHED_END
);
1584 for (x
= 0; x
< 3; x
++)
1586 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1587 keys
[x
], key_length
[x
],
1588 (time_t)50, (uint32_t)9);
1589 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1592 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1593 test_true(rc
== MEMCACHED_SUCCESS
);
1596 while ((results
= memcached_fetch_result(memc
, NULL
, &rc
)))
1599 test_true(rc
== MEMCACHED_SUCCESS
);
1600 test_true(memcached_result_key_length(results
) == memcached_result_length(results
));
1601 test_true(!memcmp(memcached_result_key_value(results
),
1602 memcached_result_value(results
),
1603 memcached_result_length(results
)));
1604 memcached_result_free(results
);
1608 return TEST_SUCCESS
;
1611 /* Count the results */
1612 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
1613 memcached_result_st
*result
,
1616 (void)ptr
; (void)result
;
1617 size_t *counter
= (size_t *)context
;
1619 *counter
= *counter
+ 1;
1621 return MEMCACHED_SUCCESS
;
1624 static test_return_t
mget_result_function(memcached_st
*memc
)
1626 memcached_return_t rc
;
1627 const char *keys
[]= {"fudge", "son", "food"};
1628 size_t key_length
[]= {5, 3, 4};
1631 memcached_execute_fn callbacks
[1];
1633 /* We need to empty the server before continueing test */
1634 rc
= memcached_flush(memc
, 0);
1635 for (x
= 0; x
< 3; x
++)
1637 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1638 keys
[x
], key_length
[x
],
1639 (time_t)50, (uint32_t)9);
1640 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1643 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1644 test_true(rc
== MEMCACHED_SUCCESS
);
1646 callbacks
[0]= &callback_counter
;
1648 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1650 test_true(counter
== 3);
1652 return TEST_SUCCESS
;
1655 static test_return_t
mget_test(memcached_st
*memc
)
1657 memcached_return_t rc
;
1658 const char *keys
[]= {"fudge", "son", "food"};
1659 size_t key_length
[]= {5, 3, 4};
1663 char return_key
[MEMCACHED_MAX_KEY
];
1664 size_t return_key_length
;
1666 size_t return_value_length
;
1668 /* We need to empty the server before continueing test */
1669 rc
= memcached_flush(memc
, 0);
1670 test_true(rc
== MEMCACHED_SUCCESS
);
1672 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1673 test_true(rc
== MEMCACHED_SUCCESS
);
1675 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1676 &return_value_length
, &flags
, &rc
)) != NULL
)
1678 test_true(return_value
);
1680 test_true(!return_value
);
1681 test_true(return_value_length
== 0);
1682 test_true(rc
== MEMCACHED_END
);
1684 for (x
= 0; x
< 3; x
++)
1686 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
1687 keys
[x
], key_length
[x
],
1688 (time_t)50, (uint32_t)9);
1689 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1692 rc
= memcached_mget(memc
, keys
, key_length
, 3);
1693 test_true(rc
== MEMCACHED_SUCCESS
);
1696 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
1697 &return_value_length
, &flags
, &rc
)))
1699 test_true(return_value
);
1700 test_true(rc
== MEMCACHED_SUCCESS
);
1701 test_true(return_key_length
== return_value_length
);
1702 test_true(!memcmp(return_value
, return_key
, return_value_length
));
1707 return TEST_SUCCESS
;
1710 static test_return_t
mget_execute(memcached_st
*memc
)
1714 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) != 0)
1718 * I only want to hit _one_ server so I know the number of requests I'm
1719 * sending in the pipeline.
1721 uint32_t number_of_hosts
= memc
->number_of_hosts
;
1722 memc
->number_of_hosts
= 1;
1724 size_t max_keys
= 20480;
1727 char **keys
= static_cast<char **>(calloc(max_keys
, sizeof(char*)));
1728 size_t *key_length
=static_cast<size_t *>(calloc(max_keys
, sizeof(size_t)));
1730 /* First add all of the items.. */
1731 char blob
[1024] = {0};
1732 memcached_return_t rc
;
1734 for (size_t x
= 0; x
< max_keys
; ++x
)
1738 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
1740 test_true(keys
[x
] != NULL
);
1741 uint64_t query_id
= memcached_query_id(memc
);
1742 rc
= memcached_add(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
1743 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1744 test_compare(query_id
+1, memcached_query_id(memc
));
1747 /* Try to get all of them with a large multiget */
1749 memcached_execute_fn callbacks
[]= { &callback_counter
};
1750 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
1751 max_keys
, callbacks
, &counter
, 1);
1753 if (rc
== MEMCACHED_SUCCESS
)
1756 uint64_t query_id
= memcached_query_id(memc
);
1757 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
1758 test_true(rc
== MEMCACHED_END
);
1759 test_compare(query_id
, memcached_query_id(memc
));
1761 /* Verify that we got all of the items */
1762 test_true(counter
== max_keys
);
1764 else if (rc
== MEMCACHED_NOT_SUPPORTED
)
1766 test_true(counter
== 0);
1770 test_fail("note: this test functions differently when in binary mode");
1773 /* Release all allocated resources */
1774 for (size_t x
= 0; x
< max_keys
; ++x
)
1781 memc
->number_of_hosts
= number_of_hosts
;
1782 return TEST_SUCCESS
;
1785 #define REGRESSION_BINARY_VS_BLOCK_COUNT 20480
1787 static test_return_t
key_setup(memcached_st
*memc
)
1791 if (pre_binary(memc
) != TEST_SUCCESS
)
1792 return TEST_SKIPPED
;
1794 global_pairs
= pairs_generate(REGRESSION_BINARY_VS_BLOCK_COUNT
, 0);
1796 return TEST_SUCCESS
;
1799 static test_return_t
key_teardown(memcached_st
*memc
)
1802 pairs_free(global_pairs
);
1804 return TEST_SUCCESS
;
1807 static test_return_t
block_add_regression(memcached_st
*memc
)
1809 /* First add all of the items.. */
1810 for (size_t x
= 0; x
< REGRESSION_BINARY_VS_BLOCK_COUNT
; ++x
)
1812 memcached_return_t rc
;
1813 char blob
[1024] = {0};
1815 rc
= memcached_add_by_key(memc
, "bob", 3, global_pairs
[x
].key
, global_pairs
[x
].key_length
, blob
, sizeof(blob
), 0, 0);
1816 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
1819 return TEST_SUCCESS
;
1822 static test_return_t
binary_add_regression(memcached_st
*memc
)
1824 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
1825 test_return_t rc
= block_add_regression(memc
);
1826 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 0);
1830 static test_return_t
get_stats_keys(memcached_st
*memc
)
1834 memcached_stat_st memc_stat
;
1835 memcached_return_t rc
;
1837 stat_list
= memcached_stat_get_keys(memc
, &memc_stat
, &rc
);
1838 test_true(rc
== MEMCACHED_SUCCESS
);
1839 for (ptr
= stat_list
; *ptr
; ptr
++)
1844 return TEST_SUCCESS
;
1847 static test_return_t
version_string_test(memcached_st
*memc
)
1849 const char *version_string
;
1852 version_string
= memcached_lib_version();
1854 test_true(!strcmp(version_string
, LIBMEMCACHED_VERSION_STRING
));
1856 return TEST_SUCCESS
;
1859 static test_return_t
get_stats(memcached_st
*memc
)
1863 memcached_return_t rc
;
1864 memcached_stat_st
*memc_stat
;
1866 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
1867 test_true(rc
== MEMCACHED_SUCCESS
);
1869 test_true(rc
== MEMCACHED_SUCCESS
);
1870 test_true(memc_stat
);
1872 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
1874 stat_list
= memcached_stat_get_keys(memc
, memc_stat
+x
, &rc
);
1875 test_true(rc
== MEMCACHED_SUCCESS
);
1876 for (ptr
= stat_list
; *ptr
; ptr
++);
1881 memcached_stat_free(NULL
, memc_stat
);
1883 return TEST_SUCCESS
;
1886 static test_return_t
add_host_test(memcached_st
*memc
)
1889 memcached_server_st
*servers
;
1890 memcached_return_t rc
;
1891 char servername
[]= "0.example.com";
1893 servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
1895 test_true(1 == memcached_server_list_count(servers
));
1897 for (x
= 2; x
< 20; x
++)
1899 char buffer
[SMALL_STRING_LEN
];
1901 snprintf(buffer
, SMALL_STRING_LEN
, "%u.example.com", 400+x
);
1902 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
1904 test_true(rc
== MEMCACHED_SUCCESS
);
1905 test_true(x
== memcached_server_list_count(servers
));
1908 rc
= memcached_server_push(memc
, servers
);
1909 test_true(rc
== MEMCACHED_SUCCESS
);
1910 rc
= memcached_server_push(memc
, servers
);
1911 test_true(rc
== MEMCACHED_SUCCESS
);
1913 memcached_server_list_free(servers
);
1915 return TEST_SUCCESS
;
1918 static memcached_return_t
clone_test_callback(memcached_st
*parent
, memcached_st
*memc_clone
)
1920 (void)parent
;(void)memc_clone
;
1921 return MEMCACHED_SUCCESS
;
1924 static memcached_return_t
cleanup_test_callback(memcached_st
*ptr
)
1927 return MEMCACHED_SUCCESS
;
1930 static test_return_t
callback_test(memcached_st
*memc
)
1932 /* Test User Data */
1936 memcached_return_t rc
;
1938 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_USER_DATA
, &x
);
1939 test_true(rc
== MEMCACHED_SUCCESS
);
1940 test_ptr
= (int *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_USER_DATA
, &rc
);
1941 test_true(*test_ptr
== x
);
1944 /* Test Clone Callback */
1946 memcached_clone_fn clone_cb
= (memcached_clone_fn
)clone_test_callback
;
1947 void *clone_cb_ptr
= *(void **)&clone_cb
;
1948 void *temp_function
= NULL
;
1949 memcached_return_t rc
;
1951 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1953 test_true(rc
== MEMCACHED_SUCCESS
);
1954 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1955 test_true(temp_function
== clone_cb_ptr
);
1958 /* Test Cleanup Callback */
1960 memcached_cleanup_fn cleanup_cb
=
1961 (memcached_cleanup_fn
)cleanup_test_callback
;
1962 void *cleanup_cb_ptr
= *(void **)&cleanup_cb
;
1963 void *temp_function
= NULL
;
1964 memcached_return_t rc
;
1966 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
,
1968 test_true(rc
== MEMCACHED_SUCCESS
);
1969 temp_function
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_CLONE_FUNCTION
, &rc
);
1970 test_true(temp_function
== cleanup_cb_ptr
);
1973 return TEST_SUCCESS
;
1976 /* We don't test the behavior itself, we test the switches */
1977 static test_return_t
behavior_test(memcached_st
*memc
)
1982 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1983 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1984 test_true(value
== 1);
1986 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
1987 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
1988 test_true(value
== 1);
1990 set
= MEMCACHED_HASH_MD5
;
1991 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
1992 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
1993 test_true(value
== MEMCACHED_HASH_MD5
);
1997 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
1998 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
1999 test_true(value
== 0);
2001 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2002 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
);
2003 test_true(value
== 0);
2005 set
= MEMCACHED_HASH_DEFAULT
;
2006 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2007 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2008 test_true(value
== MEMCACHED_HASH_DEFAULT
);
2010 set
= MEMCACHED_HASH_CRC
;
2011 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
2012 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
2013 test_true(value
== MEMCACHED_HASH_CRC
);
2015 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2016 test_true(value
> 0);
2018 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2019 test_true(value
> 0);
2021 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
);
2022 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, value
+ 1);
2023 test_true((value
+ 1) == memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
));
2025 return TEST_SUCCESS
;
2028 static test_return_t
MEMCACHED_BEHAVIOR_CORK_test(memcached_st
*memc
)
2030 memcached_return_t rc
;
2033 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
);
2034 test_true(rc
== MEMCACHED_DEPRECATED
);
2036 // Platform dependent
2038 bool value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_CORK
);
2042 return TEST_SUCCESS
;
2046 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test(memcached_st
*memc
)
2048 memcached_return_t rc
;
2052 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
, set
);
2053 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2055 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPALIVE
);
2057 if (rc
== MEMCACHED_SUCCESS
)
2059 test_true((bool)value
== set
);
2063 test_false((bool)value
== set
);
2066 return TEST_SUCCESS
;
2070 static test_return_t
MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test(memcached_st
*memc
)
2072 memcached_return_t rc
;
2076 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
, set
);
2077 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOT_SUPPORTED
);
2079 value
= (bool)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_TCP_KEEPIDLE
);
2081 if (rc
== MEMCACHED_SUCCESS
)
2083 test_true((bool)value
== set
);
2087 test_false((bool)value
== set
);
2090 return TEST_SUCCESS
;
2093 static test_return_t
fetch_all_results(memcached_st
*memc
, size_t *keys_returned
)
2095 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2096 char return_key
[MEMCACHED_MAX_KEY
];
2097 size_t return_key_length
;
2099 size_t return_value_length
;
2104 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2105 &return_value_length
, &flags
, &rc
)))
2107 test_true(return_value
);
2108 test_true(rc
== MEMCACHED_SUCCESS
);
2110 *keys_returned
= *keys_returned
+1;
2113 test_true_got(rc
== MEMCACHED_END
|| rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
2115 return TEST_SUCCESS
;
2118 /* Test case provided by Cal Haldenbrand */
2119 static test_return_t
user_supplied_bug1(memcached_st
*memc
)
2121 unsigned int setter
= 1;
2123 unsigned long long total
= 0;
2126 char randomstuff
[6 * 1024];
2127 memcached_return_t rc
;
2129 memset(randomstuff
, 0, 6 * 1024);
2131 /* We just keep looking at the same values over and over */
2134 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2135 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2139 for (uint32_t x
= 0 ; total
< 20 * 1024576 ; x
++ )
2143 size
= (uint32_t)(rand() % ( 5 * 1024 ) ) + 400;
2144 memset(randomstuff
, 0, 6 * 1024);
2145 test_true(size
< 6 * 1024); /* Being safe here */
2147 for (j
= 0 ; j
< size
;j
++)
2148 randomstuff
[j
] = (signed char) ((rand() % 26) + 97);
2151 snprintf(key
, sizeof(key
), "%u", x
);
2152 rc
= memcached_set(memc
, key
, strlen(key
),
2153 randomstuff
, strlen(randomstuff
), 10, 0);
2154 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2155 /* If we fail, lets try again */
2156 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
2157 rc
= memcached_set(memc
, key
, strlen(key
),
2158 randomstuff
, strlen(randomstuff
), 10, 0);
2159 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2162 return TEST_SUCCESS
;
2165 /* Test case provided by Cal Haldenbrand */
2166 static test_return_t
user_supplied_bug2(memcached_st
*memc
)
2168 unsigned int setter
;
2172 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2173 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2175 setter
= 20 * 1024576;
2176 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2177 setter
= 20 * 1024576;
2178 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2179 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2180 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2182 for (x
= 0, errors
= 0; total
< 20 * 1024576 ; x
++)
2185 for (uint32_t x
= 0, errors
= 0; total
< 24576 ; x
++)
2187 memcached_return_t rc
= MEMCACHED_SUCCESS
;
2188 char buffer
[SMALL_STRING_LEN
];
2193 memset(buffer
, 0, SMALL_STRING_LEN
);
2195 snprintf(buffer
, sizeof(buffer
), "%u", x
);
2196 getval
= memcached_get(memc
, buffer
, strlen(buffer
),
2197 &val_len
, &flags
, &rc
);
2198 if (rc
!= MEMCACHED_SUCCESS
)
2200 if (rc
== MEMCACHED_NOTFOUND
)
2214 return TEST_SUCCESS
;
2217 /* Do a large mget() over all the keys we think exist */
2218 #define KEY_COUNT 3000 // * 1024576
2219 static test_return_t
user_supplied_bug3(memcached_st
*memc
)
2221 memcached_return_t rc
;
2222 unsigned int setter
;
2225 size_t key_lengths
[KEY_COUNT
];
2228 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, setter
);
2229 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2231 setter
= 20 * 1024576;
2232 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
, setter
);
2233 setter
= 20 * 1024576;
2234 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
, setter
);
2235 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE
);
2236 getter
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE
);
2239 keys
= static_cast<char **>(calloc(KEY_COUNT
, sizeof(char *)));
2241 for (x
= 0; x
< KEY_COUNT
; x
++)
2245 snprintf(buffer
, 30, "%u", x
);
2246 keys
[x
]= strdup(buffer
);
2247 key_lengths
[x
]= strlen(keys
[x
]);
2250 rc
= memcached_mget(memc
, (const char **)keys
, key_lengths
, KEY_COUNT
);
2251 test_true(rc
== MEMCACHED_SUCCESS
);
2253 size_t keys_returned
;
2254 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
2256 for (x
= 0; x
< KEY_COUNT
; x
++)
2260 return TEST_SUCCESS
;
2263 /* Make sure we behave properly if server list has no values */
2264 static test_return_t
user_supplied_bug4(memcached_st
*memc
)
2266 memcached_return_t rc
;
2267 const char *keys
[]= {"fudge", "son", "food"};
2268 size_t key_length
[]= {5, 3, 4};
2271 char return_key
[MEMCACHED_MAX_KEY
];
2272 size_t return_key_length
;
2274 size_t return_value_length
;
2276 /* Here we free everything before running a bunch of mget tests */
2277 memcached_servers_reset(memc
);
2280 /* We need to empty the server before continueing test */
2281 rc
= memcached_flush(memc
, 0);
2282 test_compare(rc
, MEMCACHED_NO_SERVERS
);
2284 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2285 test_true(rc
== MEMCACHED_NO_SERVERS
);
2287 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2288 &return_value_length
, &flags
, &rc
)) != NULL
)
2290 test_true(return_value
);
2292 test_false(return_value
);
2293 test_true(return_value_length
== 0);
2294 test_true(rc
== MEMCACHED_NO_SERVERS
);
2296 for (x
= 0; x
< 3; x
++)
2298 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2299 keys
[x
], key_length
[x
],
2300 (time_t)50, (uint32_t)9);
2301 test_true(rc
== MEMCACHED_NO_SERVERS
);
2304 rc
= memcached_mget(memc
, keys
, key_length
, 3);
2305 test_true(rc
== MEMCACHED_NO_SERVERS
);
2308 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2309 &return_value_length
, &flags
, &rc
)))
2311 test_true(return_value
);
2312 test_true(rc
== MEMCACHED_SUCCESS
);
2313 test_true(return_key_length
== return_value_length
);
2314 test_memcmp(return_value
, return_key
, return_value_length
);
2319 return TEST_SUCCESS
;
2322 #define VALUE_SIZE_BUG5 1048064
2323 static test_return_t
user_supplied_bug5(memcached_st
*memc
)
2325 memcached_return_t rc
;
2326 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2327 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2328 char return_key
[MEMCACHED_MAX_KEY
];
2329 size_t return_key_length
;
2331 size_t value_length
;
2335 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2337 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2338 insert_data
[x
]= (signed char)rand();
2340 memcached_flush(memc
, 0);
2341 value
= memcached_get(memc
, keys
[0], key_length
[0],
2342 &value_length
, &flags
, &rc
);
2343 test_true(value
== NULL
);
2344 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2347 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2348 &value_length
, &flags
, &rc
)))
2350 test_true(count
== 0);
2352 for (x
= 0; x
< 4; x
++)
2354 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2355 insert_data
, VALUE_SIZE_BUG5
,
2356 (time_t)0, (uint32_t)0);
2357 test_true(rc
== MEMCACHED_SUCCESS
);
2360 for (x
= 0; x
< 10; x
++)
2362 value
= memcached_get(memc
, keys
[0], key_length
[0],
2363 &value_length
, &flags
, &rc
);
2367 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2369 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2370 &value_length
, &flags
, &rc
)))
2375 test_true(count
== 4);
2377 delete [] insert_data
;
2379 return TEST_SUCCESS
;
2382 static test_return_t
user_supplied_bug6(memcached_st
*memc
)
2384 memcached_return_t rc
;
2385 const char *keys
[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
2386 size_t key_length
[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
2387 char return_key
[MEMCACHED_MAX_KEY
];
2388 size_t return_key_length
;
2390 size_t value_length
;
2394 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2396 for (x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2397 insert_data
[x
]= (signed char)rand();
2399 memcached_flush(memc
, 0);
2400 value
= memcached_get(memc
, keys
[0], key_length
[0],
2401 &value_length
, &flags
, &rc
);
2402 test_true(value
== NULL
);
2403 test_true(rc
== MEMCACHED_NOTFOUND
);
2404 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2405 test_true(rc
== MEMCACHED_SUCCESS
);
2408 while ((value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2409 &value_length
, &flags
, &rc
)))
2411 test_true(count
== 0);
2412 test_true(rc
== MEMCACHED_END
);
2414 for (x
= 0; x
< 4; x
++)
2416 rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2417 insert_data
, VALUE_SIZE_BUG5
,
2418 (time_t)0, (uint32_t)0);
2419 test_true(rc
== MEMCACHED_SUCCESS
);
2422 for (x
= 0; x
< 2; x
++)
2424 value
= memcached_get(memc
, keys
[0], key_length
[0],
2425 &value_length
, &flags
, &rc
);
2429 rc
= memcached_mget(memc
, keys
, key_length
, 4);
2430 test_true(rc
== MEMCACHED_SUCCESS
);
2432 /* We test for purge of partial complete fetches */
2433 for (count
= 3; count
; count
--)
2435 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2436 &value_length
, &flags
, &rc
);
2437 test_true(rc
== MEMCACHED_SUCCESS
);
2438 test_true(!(memcmp(value
, insert_data
, value_length
)));
2439 test_true(value_length
);
2443 delete [] insert_data
;
2445 return TEST_SUCCESS
;
2448 static test_return_t
user_supplied_bug8(memcached_st
*memc
)
2450 memcached_return_t rc
;
2452 memcached_st
*memc_clone
;
2454 memcached_server_st
*servers
;
2455 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";
2458 servers
= memcached_servers_parse(server_list
);
2461 mine
= memcached_create(NULL
);
2462 rc
= memcached_server_push(mine
, servers
);
2463 test_true(rc
== MEMCACHED_SUCCESS
);
2464 memcached_server_list_free(servers
);
2467 memc_clone
= memcached_clone(NULL
, mine
);
2469 memcached_quit(mine
);
2470 memcached_quit(memc_clone
);
2473 memcached_free(mine
);
2474 memcached_free(memc_clone
);
2476 return TEST_SUCCESS
;
2479 /* Test flag store/retrieve */
2480 static test_return_t
user_supplied_bug7(memcached_st
*memc
)
2482 const char *keys
= "036790384900";
2483 size_t key_length
= strlen(keys
);
2484 char return_key
[MEMCACHED_MAX_KEY
];
2485 size_t return_key_length
;
2487 size_t value_length
;
2489 char *insert_data
= new (std::nothrow
) char[VALUE_SIZE_BUG5
];
2491 for (unsigned int x
= 0; x
< VALUE_SIZE_BUG5
; x
++)
2492 insert_data
[x
]= (signed char)rand();
2494 memcached_flush(memc
, 0);
2497 memcached_return_t rc
= memcached_set(memc
, keys
, key_length
,
2498 insert_data
, VALUE_SIZE_BUG5
,
2500 test_true(rc
== MEMCACHED_SUCCESS
);
2503 value
= memcached_get(memc
, keys
, key_length
,
2504 &value_length
, &flags
, &rc
);
2505 test_true(flags
== 245);
2509 rc
= memcached_mget(memc
, &keys
, &key_length
, 1);
2512 value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2513 &value_length
, &flags
, &rc
);
2514 test_compare(245, flags
);
2517 delete [] insert_data
;
2520 return TEST_SUCCESS
;
2523 static test_return_t
user_supplied_bug9(memcached_st
*memc
)
2525 const char *keys
[]= {"UDATA:edevil@sapo.pt", "fudge&*@#", "for^#@&$not"};
2526 size_t key_length
[3];
2530 char return_key
[MEMCACHED_MAX_KEY
];
2531 size_t return_key_length
;
2533 size_t return_value_length
;
2536 key_length
[0]= strlen("UDATA:edevil@sapo.pt");
2537 key_length
[1]= strlen("fudge&*@#");
2538 key_length
[2]= strlen("for^#@&$not");
2541 for (unsigned int x
= 0; x
< 3; x
++)
2543 memcached_return_t rc
= memcached_set(memc
, keys
[x
], key_length
[x
],
2544 keys
[x
], key_length
[x
],
2545 (time_t)50, (uint32_t)9);
2546 test_true(rc
== MEMCACHED_SUCCESS
);
2549 memcached_return_t rc
= memcached_mget(memc
, keys
, key_length
, 3);
2550 test_true(rc
== MEMCACHED_SUCCESS
);
2552 /* We need to empty the server before continueing test */
2553 while ((return_value
= memcached_fetch(memc
, return_key
, &return_key_length
,
2554 &return_value_length
, &flags
, &rc
)) != NULL
)
2556 test_true(return_value
);
2560 test_compare(3, count
);
2562 return TEST_SUCCESS
;
2565 /* We are testing with aggressive timeout to get failures */
2566 static test_return_t
user_supplied_bug10(memcached_st
*memc
)
2568 const char *key
= "foo";
2570 size_t value_length
= 512;
2573 unsigned int set
= 1;
2574 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2577 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2578 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2580 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2583 value
= (char*)malloc(value_length
* sizeof(char));
2585 for (x
= 0; x
< value_length
; x
++)
2586 value
[x
]= (char) (x
% 127);
2588 for (x
= 1; x
<= 100000; ++x
)
2590 memcached_return_t rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2592 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_WRITE_FAILURE
||
2593 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
);
2595 if (rc
== MEMCACHED_WRITE_FAILURE
|| rc
== MEMCACHED_TIMEOUT
)
2600 memcached_free(mclone
);
2602 return TEST_SUCCESS
;
2606 We are looking failures in the async protocol
2608 static test_return_t
user_supplied_bug11(memcached_st
*memc
)
2610 const char *key
= "foo";
2612 size_t value_length
= 512;
2615 memcached_return_t rc
;
2616 unsigned int set
= 1;
2618 memcached_st
*mclone
= memcached_clone(NULL
, memc
);
2620 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, set
);
2621 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, set
);
2623 memcached_behavior_set(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
,
2626 timeout
= (int32_t)memcached_behavior_get(mclone
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
2628 test_true(timeout
== -1);
2630 value
= (char*)malloc(value_length
* sizeof(char));
2632 for (x
= 0; x
< value_length
; x
++)
2633 value
[x
]= (char) (x
% 127);
2635 for (x
= 1; x
<= 100000; ++x
)
2637 rc
= memcached_set(mclone
, key
, key_len
,value
, value_length
, 0, 0);
2641 memcached_free(mclone
);
2643 return TEST_SUCCESS
;
2647 Bug found where incr was not returning MEMCACHED_NOTFOUND when object did not exist.
2649 static test_return_t
user_supplied_bug12(memcached_st
*memc
)
2651 memcached_return_t rc
;
2653 size_t value_length
;
2655 uint64_t number_value
;
2657 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2658 &value_length
, &flags
, &rc
);
2659 test_true(value
== NULL
);
2660 test_true(rc
== MEMCACHED_NOTFOUND
);
2662 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2665 test_true(value
== NULL
);
2666 /* The binary protocol will set the key if it doesn't exist */
2667 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1)
2669 test_true(rc
== MEMCACHED_SUCCESS
);
2673 test_true(rc
== MEMCACHED_NOTFOUND
);
2676 rc
= memcached_set(memc
, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
2678 value
= memcached_get(memc
, "autoincrement", strlen("autoincrement"),
2679 &value_length
, &flags
, &rc
);
2681 test_true(rc
== MEMCACHED_SUCCESS
);
2684 rc
= memcached_increment(memc
, "autoincrement", strlen("autoincrement"),
2686 test_true(number_value
== 2);
2687 test_true(rc
== MEMCACHED_SUCCESS
);
2689 return TEST_SUCCESS
;
2693 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2694 set key34567890 0 0 8169 \r\n is sent followed by buffer of size 8169, followed by 8169
2696 static test_return_t
user_supplied_bug13(memcached_st
*memc
)
2698 char key
[] = "key34567890";
2699 memcached_return_t rc
;
2700 size_t overflowSize
;
2702 char commandFirst
[]= "set key34567890 0 0 ";
2703 char commandLast
[] = " \r\n"; /* first line of command sent to server */
2704 size_t commandLength
;
2707 commandLength
= strlen(commandFirst
) + strlen(commandLast
) + 4; /* 4 is number of characters in size, probably 8196 */
2709 overflowSize
= MEMCACHED_MAX_BUFFER
- commandLength
;
2711 for (testSize
= overflowSize
- 1; testSize
< overflowSize
+ 1; testSize
++)
2713 char *overflow
= new (std::nothrow
) char[testSize
];
2714 test_true(overflow
);
2716 memset(overflow
, 'x', testSize
);
2717 rc
= memcached_set(memc
, key
, strlen(key
),
2718 overflow
, testSize
, 0, 0);
2719 test_true(rc
== MEMCACHED_SUCCESS
);
2723 return TEST_SUCCESS
;
2728 Test values of many different sizes
2729 Bug found where command total one more than MEMCACHED_MAX_BUFFER
2730 set key34567890 0 0 8169 \r\n
2731 is sent followed by buffer of size 8169, followed by 8169
2733 static test_return_t
user_supplied_bug14(memcached_st
*memc
)
2736 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, setter
);
2737 memcached_return_t rc
;
2738 const char *key
= "foo";
2740 size_t value_length
= 18000;
2742 size_t string_length
;
2745 size_t current_length
;
2747 value
= (char*)malloc(value_length
);
2750 for (x
= 0; x
< value_length
; x
++)
2751 value
[x
] = (char) (x
% 127);
2753 for (current_length
= 0; current_length
< value_length
; current_length
++)
2755 rc
= memcached_set(memc
, key
, strlen(key
),
2756 value
, current_length
,
2757 (time_t)0, (uint32_t)0);
2758 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
2760 string
= memcached_get(memc
, key
, strlen(key
),
2761 &string_length
, &flags
, &rc
);
2763 test_true(rc
== MEMCACHED_SUCCESS
);
2764 test_true(string_length
== current_length
);
2765 test_true(!memcmp(string
, value
, string_length
));
2772 return TEST_SUCCESS
;
2776 Look for zero length value problems
2778 static test_return_t
user_supplied_bug15(memcached_st
*memc
)
2781 memcached_return_t rc
;
2782 const char *key
= "mykey";
2787 for (x
= 0; x
< 2; x
++)
2789 rc
= memcached_set(memc
, key
, strlen(key
),
2791 (time_t)0, (uint32_t)0);
2793 test_true(rc
== MEMCACHED_SUCCESS
);
2795 value
= memcached_get(memc
, key
, strlen(key
),
2796 &length
, &flags
, &rc
);
2798 test_true(rc
== MEMCACHED_SUCCESS
);
2799 test_true(value
== NULL
);
2800 test_true(length
== 0);
2801 test_true(flags
== 0);
2803 value
= memcached_get(memc
, key
, strlen(key
),
2804 &length
, &flags
, &rc
);
2806 test_true(rc
== MEMCACHED_SUCCESS
);
2807 test_true(value
== NULL
);
2808 test_true(length
== 0);
2809 test_true(flags
== 0);
2812 return TEST_SUCCESS
;
2815 /* Check the return sizes on FLAGS to make sure it stores 32bit unsigned values correctly */
2816 static test_return_t
user_supplied_bug16(memcached_st
*memc
)
2818 memcached_return_t rc
;
2819 const char *key
= "mykey";
2824 rc
= memcached_set(memc
, key
, strlen(key
),
2826 (time_t)0, UINT32_MAX
);
2828 test_true(rc
== MEMCACHED_SUCCESS
);
2830 value
= memcached_get(memc
, key
, strlen(key
),
2831 &length
, &flags
, &rc
);
2833 test_true(rc
== MEMCACHED_SUCCESS
);
2834 test_true(value
== NULL
);
2835 test_true(length
== 0);
2836 test_true(flags
== UINT32_MAX
);
2838 return TEST_SUCCESS
;
2841 #if !defined(__sun) && !defined(__OpenBSD__)
2842 /* Check the validity of chinese key*/
2843 static test_return_t
user_supplied_bug17(memcached_st
*memc
)
2845 memcached_return_t rc
;
2846 const char *key
= "豆瓣";
2847 const char *value
="我们在炎热抑郁的夏天无法停止豆瓣";
2852 rc
= memcached_set(memc
, key
, strlen(key
),
2853 value
, strlen(value
),
2856 test_true(rc
== MEMCACHED_SUCCESS
);
2858 value2
= memcached_get(memc
, key
, strlen(key
),
2859 &length
, &flags
, &rc
);
2861 test_true(length
==strlen(value
));
2862 test_true(rc
== MEMCACHED_SUCCESS
);
2863 test_true(memcmp(value
, value2
, length
)==0);
2866 return TEST_SUCCESS
;
2874 static test_return_t
user_supplied_bug19(memcached_st
*not_used
)
2877 const memcached_server_st
*server
;
2878 memcached_return_t res
;
2882 memc
= memcached_create(NULL
);
2883 memcached_server_add_with_weight(memc
, "localhost", 11311, 100);
2884 memcached_server_add_with_weight(memc
, "localhost", 11312, 100);
2886 server
= memcached_server_by_key(memc
, "a", 1, &res
);
2888 memcached_free(memc
);
2890 return TEST_SUCCESS
;
2893 /* CAS test from Andei */
2894 static test_return_t
user_supplied_bug20(memcached_st
*memc
)
2896 memcached_return_t status
;
2897 memcached_result_st
*result
, result_obj
;
2898 const char *key
= "abc";
2899 size_t key_len
= strlen("abc");
2900 const char *value
= "foobar";
2901 size_t value_len
= strlen(value
);
2903 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
2905 status
= memcached_set(memc
, key
, key_len
, value
, value_len
, (time_t)0, (uint32_t)0);
2906 test_true(status
== MEMCACHED_SUCCESS
);
2908 status
= memcached_mget(memc
, &key
, &key_len
, 1);
2909 test_true(status
== MEMCACHED_SUCCESS
);
2911 result
= memcached_result_create(memc
, &result_obj
);
2914 memcached_result_create(memc
, &result_obj
);
2915 result
= memcached_fetch_result(memc
, &result_obj
, &status
);
2918 test_true(status
== MEMCACHED_SUCCESS
);
2920 memcached_result_free(result
);
2922 return TEST_SUCCESS
;
2925 #include "ketama_test_cases.h"
2926 static test_return_t
user_supplied_bug18(memcached_st
*trash
)
2928 memcached_return_t rc
;
2931 memcached_server_st
*server_pool
;
2936 memc
= memcached_create(NULL
);
2939 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
2940 test_true(rc
== MEMCACHED_SUCCESS
);
2942 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
2943 test_true(value
== 1);
2945 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
2946 test_true(rc
== MEMCACHED_SUCCESS
);
2948 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
2949 test_true(value
== MEMCACHED_HASH_MD5
);
2951 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");
2952 memcached_server_push(memc
, server_pool
);
2954 /* verify that the server list was parsed okay. */
2955 test_true(memcached_server_count(memc
) == 8);
2956 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
2957 test_true(server_pool
[0].port
== 11211);
2958 test_true(server_pool
[0].weight
== 600);
2959 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
2960 test_true(server_pool
[2].port
== 11211);
2961 test_true(server_pool
[2].weight
== 200);
2962 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
2963 test_true(server_pool
[7].port
== 11211);
2964 test_true(server_pool
[7].weight
== 100);
2966 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
2967 * us test the boundary wraparound.
2969 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
2971 /* verify the standard ketama set. */
2972 for (x
= 0; x
< 99; x
++)
2974 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
2976 memcached_server_instance_st instance
=
2977 memcached_server_instance_by_position(memc
, server_idx
);
2979 const char *hostname
= memcached_server_name(instance
);
2980 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
2983 memcached_server_list_free(server_pool
);
2984 memcached_free(memc
);
2986 return TEST_SUCCESS
;
2989 /* Large mget() of missing keys with binary proto
2991 * If many binary quiet commands (such as getq's in an mget) fill the output
2992 * buffer and the server chooses not to respond, memcached_flush hangs. See
2993 * http://lists.tangent.org/pipermail/libmemcached/2009-August/000918.html
2996 /* sighandler_t function that always asserts false */
2997 static void fail(int unused
)
3004 static test_return_t
_user_supplied_bug21(memcached_st
* memc
, size_t key_count
)
3009 return TEST_SKIPPED
;
3011 void (*oldalarm
)(int);
3013 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3014 test_true(memc_clone
);
3016 /* only binproto uses getq for mget */
3017 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3019 /* empty the cache to ensure misses (hence non-responses) */
3020 memcached_return_t rc
= memcached_flush(memc_clone
, 0);
3021 test_true(rc
== MEMCACHED_SUCCESS
);
3023 size_t* key_lengths
= new (std::nothrow
) size_t[key_count
];
3024 test_true(key_lengths
);
3025 char **keys
= static_cast<char **>(calloc(key_count
, sizeof(char *)));
3027 for (unsigned int x
= 0; x
< key_count
; x
++)
3031 snprintf(buffer
, 30, "%u", x
);
3032 keys
[x
]= strdup(buffer
);
3033 key_lengths
[x
]= strlen(keys
[x
]);
3036 oldalarm
= signal(SIGALRM
, fail
);
3039 test_true_got(memcached_success(memcached_mget(memc_clone
, (const char **)keys
, key_lengths
, key_count
)), memcached_last_error_message(memc_clone
));
3042 signal(SIGALRM
, oldalarm
);
3044 size_t keys_returned
;
3045 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3047 for (unsigned int x
= 0; x
< key_count
; x
++)
3052 delete [] key_lengths
;
3054 memcached_free(memc_clone
);
3056 return TEST_SUCCESS
;
3060 static test_return_t
user_supplied_bug21(memcached_st
*memc
)
3062 test_return_t test_rc
;
3063 test_rc
= pre_binary(memc
);
3065 if (test_rc
!= TEST_SUCCESS
)
3070 /* should work as of r580 */
3071 rc
= _user_supplied_bug21(memc
, 10);
3072 test_true(rc
== TEST_SUCCESS
);
3074 /* should fail as of r580 */
3075 rc
= _user_supplied_bug21(memc
, 1000);
3076 test_true(rc
== TEST_SUCCESS
);
3078 return TEST_SUCCESS
;
3081 static test_return_t
auto_eject_hosts(memcached_st
*trash
)
3084 memcached_server_instance_st instance
;
3086 memcached_return_t rc
;
3087 memcached_st
*memc
= memcached_create(NULL
);
3090 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3091 test_true(rc
== MEMCACHED_SUCCESS
);
3093 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3094 test_true(value
== 1);
3096 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3097 test_true(rc
== MEMCACHED_SUCCESS
);
3099 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3100 test_true(value
== MEMCACHED_HASH_MD5
);
3102 /* server should be removed when in delay */
3103 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, 1);
3104 test_true(rc
== MEMCACHED_SUCCESS
);
3106 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
3107 test_true(value
== 1);
3109 memcached_server_st
*server_pool
;
3110 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");
3111 memcached_server_push(memc
, server_pool
);
3113 /* verify that the server list was parsed okay. */
3114 test_true(memcached_server_count(memc
) == 8);
3115 test_true(strcmp(server_pool
[0].hostname
, "10.0.1.1") == 0);
3116 test_true(server_pool
[0].port
== 11211);
3117 test_true(server_pool
[0].weight
== 600);
3118 test_true(strcmp(server_pool
[2].hostname
, "10.0.1.3") == 0);
3119 test_true(server_pool
[2].port
== 11211);
3120 test_true(server_pool
[2].weight
== 200);
3121 test_true(strcmp(server_pool
[7].hostname
, "10.0.1.8") == 0);
3122 test_true(server_pool
[7].port
== 11211);
3123 test_true(server_pool
[7].weight
== 100);
3125 instance
= memcached_server_instance_by_position(memc
, 2);
3126 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) + 15;
3127 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3130 This would not work if there were only two hosts.
3132 for (size_t x
= 0; x
< 99; x
++)
3134 memcached_autoeject(memc
);
3135 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3136 test_true(server_idx
!= 2);
3139 /* and re-added when it's back. */
3140 ((memcached_server_write_instance_st
)instance
)->next_retry
= time(NULL
) - 1;
3141 memc
->ketama
.next_distribution_rebuild
= time(NULL
) - 1;
3142 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
,
3143 memc
->distribution
);
3144 for (size_t x
= 0; x
< 99; x
++)
3146 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
3147 // We re-use instance from above.
3149 memcached_server_instance_by_position(memc
, server_idx
);
3150 const char *hostname
= memcached_server_name(instance
);
3151 test_true(strcmp(hostname
, ketama_test_cases
[x
].server
) == 0);
3154 memcached_server_list_free(server_pool
);
3155 memcached_free(memc
);
3157 return TEST_SUCCESS
;
3160 static test_return_t
output_ketama_weighted_keys(memcached_st
*trash
)
3164 memcached_return_t rc
;
3165 memcached_st
*memc
= memcached_create(NULL
);
3169 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3170 test_true(rc
== MEMCACHED_SUCCESS
);
3172 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3173 test_true(value
== 1);
3175 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3176 test_true(rc
== MEMCACHED_SUCCESS
);
3178 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3179 test_true(value
== MEMCACHED_HASH_MD5
);
3182 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
3184 memcached_server_st
*server_pool
;
3185 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");
3186 memcached_server_push(memc
, server_pool
);
3188 // @todo this needs to be refactored to actually test something.
3191 if ((fp
= fopen("ketama_keys.txt", "w")))
3195 printf("cannot write to file ketama_keys.txt");
3196 return TEST_FAILURE
;
3199 for (int x
= 0; x
< 10000; x
++)
3202 snprintf(key
, sizeof(key
), "%d", x
);
3204 uint32_t server_idx
= memcached_generate_hash(memc
, key
, strlen(key
));
3205 char *hostname
= memc
->hosts
[server_idx
].hostname
;
3206 in_port_t port
= memc
->hosts
[server_idx
].port
;
3207 fprintf(fp
, "key %s is on host /%s:%u\n", key
, hostname
, port
);
3208 memcached_server_instance_st instance
=
3209 memcached_server_instance_by_position(memc
, host_index
);
3213 memcached_server_list_free(server_pool
);
3214 memcached_free(memc
);
3216 return TEST_SUCCESS
;
3220 static test_return_t
result_static(memcached_st
*memc
)
3222 memcached_result_st result
;
3223 memcached_result_st
*result_ptr
;
3225 result_ptr
= memcached_result_create(memc
, &result
);
3226 test_true(result
.options
.is_allocated
== false);
3227 test_true(memcached_is_initialized(&result
) == true);
3228 test_true(result_ptr
);
3229 test_true(result_ptr
== &result
);
3231 memcached_result_free(&result
);
3233 test_true(result
.options
.is_allocated
== false);
3234 test_true(memcached_is_initialized(&result
) == false);
3236 return TEST_SUCCESS
;
3239 static test_return_t
result_alloc(memcached_st
*memc
)
3241 memcached_result_st
*result_ptr
;
3243 result_ptr
= memcached_result_create(memc
, NULL
);
3244 test_true(result_ptr
);
3245 test_true(result_ptr
->options
.is_allocated
== true);
3246 test_true(memcached_is_initialized(result_ptr
) == true);
3247 memcached_result_free(result_ptr
);
3249 return TEST_SUCCESS
;
3252 static test_return_t
cleanup_pairs(memcached_st
*memc
)
3255 pairs_free(global_pairs
);
3257 return TEST_SUCCESS
;
3260 static test_return_t
generate_pairs(memcached_st
*memc
)
3263 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
3264 global_count
= GLOBAL_COUNT
;
3266 for (size_t x
= 0; x
< global_count
; x
++)
3268 global_keys
[x
]= global_pairs
[x
].key
;
3269 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3272 return TEST_SUCCESS
;
3275 static test_return_t
generate_large_pairs(memcached_st
*)
3277 global_pairs
= pairs_generate(GLOBAL2_COUNT
, MEMCACHED_MAX_BUFFER
+10);
3278 global_count
= GLOBAL2_COUNT
;
3280 for (size_t x
= 0; x
< global_count
; x
++)
3282 global_keys
[x
]= global_pairs
[x
].key
;
3283 global_keys_length
[x
]= global_pairs
[x
].key_length
;
3286 return TEST_SUCCESS
;
3289 static test_return_t
generate_data(memcached_st
*memc
)
3291 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3293 test_true(check_execute
== global_count
);
3295 return TEST_SUCCESS
;
3298 static test_return_t
generate_data_with_stats(memcached_st
*memc
)
3300 uint32_t host_index
= 0;
3301 unsigned int check_execute
= execute_set(memc
, global_pairs
, global_count
);
3303 test_true(check_execute
== global_count
);
3305 // @todo hosts used size stats
3306 memcached_return_t rc
;
3307 memcached_stat_st
*stat_p
= memcached_stat(memc
, NULL
, &rc
);
3310 for (host_index
= 0; host_index
< SERVERS_TO_CREATE
; host_index
++)
3312 /* This test was changes so that "make test" would work properlly */
3314 memcached_server_instance_st instance
=
3315 memcached_server_instance_by_position(memc
, host_index
);
3317 printf("\nserver %u|%s|%u bytes: %llu\n", host_index
, instance
->hostname
, instance
->port
, (unsigned long long)(stat_p
+ host_index
)->bytes
);
3319 test_true((unsigned long long)(stat_p
+ host_index
)->bytes
);
3322 memcached_stat_free(NULL
, stat_p
);
3324 return TEST_SUCCESS
;
3326 static test_return_t
generate_buffer_data(memcached_st
*memc
)
3331 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3332 generate_data(memc
);
3334 return TEST_SUCCESS
;
3337 static test_return_t
get_read_count(memcached_st
*memc
)
3339 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
3340 test_true(memc_clone
);
3342 memcached_server_add_with_weight(memc_clone
, "localhost", 6666, 0);
3346 size_t return_value_length
;
3350 for (size_t x
= count
= 0; x
< global_count
; x
++)
3352 memcached_return_t rc
;
3353 return_value
= memcached_get(memc_clone
, global_keys
[x
], global_keys_length
[x
],
3354 &return_value_length
, &flags
, &rc
);
3355 if (rc
== MEMCACHED_SUCCESS
)
3364 memcached_free(memc_clone
);
3366 return TEST_SUCCESS
;
3369 static test_return_t
get_read(memcached_st
*memc
)
3371 for (size_t x
= 0; x
< global_count
; x
++)
3373 size_t return_value_length
;
3375 memcached_return_t rc
;
3376 char *return_value
= memcached_get(memc
, global_keys
[x
], global_keys_length
[x
],
3377 &return_value_length
, &flags
, &rc
);
3379 test_true(return_value);
3380 test_true(rc == MEMCACHED_SUCCESS);
3382 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
3386 return TEST_SUCCESS
;
3389 static test_return_t
mget_read(memcached_st
*memc
)
3392 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3393 return TEST_SKIPPED
;
3395 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3397 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3399 // Go fetch the keys and test to see if all of them were returned
3401 size_t keys_returned
;
3402 test_true(fetch_all_results(memc
, &keys_returned
) == TEST_SUCCESS
);
3404 snprintf(buffer
, sizeof(buffer
), "%lu", (unsigned long)keys_returned
);
3405 test_true_got(global_count
== keys_returned
, buffer
);
3409 return TEST_SUCCESS
;
3412 static test_return_t
mget_read_result(memcached_st
*memc
)
3415 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3416 return TEST_SKIPPED
;
3418 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3420 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3422 /* Turn this into a help function */
3424 memcached_result_st results_obj
;
3425 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
3427 while ((results
= memcached_fetch_result(memc
, &results_obj
, &rc
)))
3430 test_true(rc
== MEMCACHED_SUCCESS
);
3433 memcached_result_free(&results_obj
);
3436 return TEST_SUCCESS
;
3439 static test_return_t
mget_read_function(memcached_st
*memc
)
3442 if (not libmemcached_util_version_check(memc
, 1, 4, 4))
3443 return TEST_SKIPPED
;
3445 memcached_return_t rc
= memcached_mget(memc
, global_keys
, global_keys_length
, global_count
);
3447 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
3449 memcached_execute_fn callbacks
[]= { &callback_counter
};
3451 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
3453 return TEST_SUCCESS
;
3456 static test_return_t
delete_generate(memcached_st
*memc
)
3458 for (size_t x
= 0; x
< global_count
; x
++)
3460 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3463 return TEST_SUCCESS
;
3466 static test_return_t
delete_buffer_generate(memcached_st
*memc
)
3469 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, latch
);
3471 for (size_t x
= 0; x
< global_count
; x
++)
3473 (void)memcached_delete(memc
, global_keys
[x
], global_keys_length
[x
], (time_t)0);
3476 return TEST_SUCCESS
;
3479 static test_return_t
add_host_test1(memcached_st
*memc
)
3481 memcached_return_t rc
;
3482 char servername
[]= "0.example.com";
3484 memcached_server_st
*servers
= memcached_server_list_append_with_weight(NULL
, servername
, 400, 0, &rc
);
3486 test_compare(1, memcached_server_list_count(servers
));
3488 for (size_t x
= 2; x
< 20; x
++)
3490 char buffer
[SMALL_STRING_LEN
];
3492 snprintf(buffer
, SMALL_STRING_LEN
, "%lu.example.com", (unsigned long)(400 +x
));
3493 servers
= memcached_server_list_append_with_weight(servers
, buffer
, 401, 0,
3495 test_compare(MEMCACHED_SUCCESS
, rc
);
3496 test_compare(x
, memcached_server_list_count(servers
));
3499 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3500 test_compare(MEMCACHED_SUCCESS
, memcached_server_push(memc
, servers
));
3502 memcached_server_list_free(servers
);
3504 return TEST_SUCCESS
;
3507 static test_return_t
pre_nonblock(memcached_st
*memc
)
3509 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3511 return TEST_SUCCESS
;
3514 static test_return_t
pre_cork(memcached_st
*memc
)
3517 return TEST_SKIPPED
;
3520 if (memcached_success(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CORK
, set
)))
3521 return TEST_SUCCESS
;
3523 return TEST_SKIPPED
;
3526 static test_return_t
pre_cork_and_nonblock(memcached_st
*memc
)
3529 return TEST_SKIPPED
;
3531 test_return_t test_rc
;
3532 if ((test_rc
= pre_cork(memc
)) != TEST_SUCCESS
)
3535 return pre_nonblock(memc
);
3538 static test_return_t
pre_nonblock_binary(memcached_st
*memc
)
3540 memcached_return_t rc
= MEMCACHED_FAILURE
;
3541 memcached_st
*memc_clone
;
3543 memc_clone
= memcached_clone(NULL
, memc
);
3544 test_true(memc_clone
);
3545 // The memcached_version needs to be done on a clone, because the server
3546 // will not toggle protocol on an connection.
3547 memcached_version(memc_clone
);
3549 if (libmemcached_util_version_check(memc_clone
, 1, 4, 4))
3551 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
3552 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3553 test_true(rc
== MEMCACHED_SUCCESS
);
3554 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3558 return TEST_SKIPPED
;
3561 memcached_free(memc_clone
);
3563 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3566 static test_return_t
pre_murmur(memcached_st
*memc
)
3568 #ifdef HAVE_MURMUR_HASH
3569 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3570 return TEST_SUCCESS
;
3573 return TEST_SKIPPED
;
3577 static test_return_t
pre_jenkins(memcached_st
*memc
)
3579 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_JENKINS
);
3581 return TEST_SUCCESS
;
3585 static test_return_t
pre_md5(memcached_st
*memc
)
3587 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MD5
);
3589 return TEST_SUCCESS
;
3592 static test_return_t
pre_crc(memcached_st
*memc
)
3594 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_CRC
);
3596 return TEST_SUCCESS
;
3599 static test_return_t
pre_hsieh(memcached_st
*memc
)
3601 #ifdef HAVE_HSIEH_HASH
3602 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_HSIEH
);
3603 return TEST_SUCCESS
;
3606 return TEST_SKIPPED
;
3610 static test_return_t
pre_hash_fnv1_64(memcached_st
*memc
)
3612 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_MURMUR
);
3614 return TEST_SUCCESS
;
3617 static test_return_t
pre_hash_fnv1a_64(memcached_st
*memc
)
3619 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_64
);
3621 return TEST_SUCCESS
;
3624 static test_return_t
pre_hash_fnv1_32(memcached_st
*memc
)
3626 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1_32
);
3628 return TEST_SUCCESS
;
3631 static test_return_t
pre_hash_fnv1a_32(memcached_st
*memc
)
3633 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, (uint64_t)MEMCACHED_HASH_FNV1A_32
);
3635 return TEST_SUCCESS
;
3638 static test_return_t
pre_behavior_ketama(memcached_st
*memc
)
3640 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA
, 1);
3641 test_true(rc
== MEMCACHED_SUCCESS
);
3643 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA
);
3644 test_true(value
== 1);
3646 return TEST_SUCCESS
;
3649 static test_return_t
pre_behavior_ketama_weighted(memcached_st
*memc
)
3651 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
3652 test_true(rc
== MEMCACHED_SUCCESS
);
3654 uint64_t value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
3655 test_true(value
== 1);
3657 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
, MEMCACHED_HASH_MD5
);
3658 test_true(rc
== MEMCACHED_SUCCESS
);
3660 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_HASH
);
3661 test_true(value
== MEMCACHED_HASH_MD5
);
3663 return TEST_SUCCESS
;
3667 @note This should be testing to see if the server really supports the binary protocol.
3669 static test_return_t
pre_binary(memcached_st
*memc
)
3671 memcached_return_t rc
= MEMCACHED_FAILURE
;
3673 if (libmemcached_util_version_check(memc
, 1, 4, 4))
3675 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3676 test_true(rc
== MEMCACHED_SUCCESS
);
3677 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
3680 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3683 static test_return_t
pre_sasl(memcached_st
*memc
)
3685 memcached_return_t rc
= MEMCACHED_FAILURE
;
3687 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
3688 const char *server
= getenv("LIBMEMCACHED_TEST_SASL_SERVER");
3689 const char *user
= getenv("LIBMEMCACHED_TEST_SASL_USERNAME");
3690 const char *pass
= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD");
3692 if (server
and user
and pass
)
3694 memcached_server_st
*servers
= memcached_servers_parse(server
);
3696 memcached_servers_reset(memc
);
3697 test_true(memcached_server_push(memc
, servers
) == MEMCACHED_SUCCESS
);
3698 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
3699 rc
= memcached_set_sasl_auth_data(memc
, user
, pass
);
3700 test_true(rc
== MEMCACHED_SUCCESS
);
3706 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3709 static test_return_t
pre_replication(memcached_st
*memc
)
3711 test_true(TEST_SUCCESS
== pre_binary(memc
));
3714 * Make sure that we store the item on all servers
3715 * (master + replicas == number of servers)
3717 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
,
3718 memcached_server_count(memc
) - 1);
3719 test_true(rc
== MEMCACHED_SUCCESS
);
3720 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
) == memcached_server_count(memc
) - 1);
3722 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
3726 static test_return_t
pre_replication_noblock(memcached_st
*memc
)
3728 test_compare(TEST_SUCCESS
, pre_replication(memc
));
3730 return pre_nonblock(memc
);
3734 static void my_free(const memcached_st
*ptr
, void *mem
, void *context
)
3738 #ifdef HARD_MALLOC_TESTS
3739 void *real_ptr
= (mem
== NULL
) ? mem
: (void*)((caddr_t
)mem
- 8);
3747 static void *my_malloc(const memcached_st
*ptr
, const size_t size
, void *context
)
3751 #ifdef HARD_MALLOC_TESTS
3752 void *ret
= malloc(size
+ 8);
3755 ret
= (void*)((caddr_t
)ret
+ 8);
3758 void *ret
= malloc(size
);
3763 memset(ret
, 0xff, size
);
3770 static void *my_realloc(const memcached_st
*ptr
, void *mem
, const size_t size
, void *)
3772 #ifdef HARD_MALLOC_TESTS
3773 void *real_ptr
= (mem
== NULL
) ? NULL
: (void*)((caddr_t
)mem
- 8);
3774 void *nmem
= realloc(real_ptr
, size
+ 8);
3779 ret
= (void*)((caddr_t
)nmem
+ 8);
3785 return realloc(mem
, size
);
3790 static void *my_calloc(const memcached_st
*ptr
, size_t nelem
, const size_t size
, void *)
3792 #ifdef HARD_MALLOC_TESTS
3793 void *mem
= my_malloc(ptr
, nelem
* size
);
3796 memset(mem
, 0, nelem
* size
);
3802 return calloc(nelem
, size
);
3806 static test_return_t
set_prefix(memcached_st
*memc
)
3808 memcached_return_t rc
;
3809 const char *key
= "mine";
3812 /* Make sure be default none exists */
3813 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3814 test_true(rc
== MEMCACHED_FAILURE
);
3816 /* Test a clean set */
3817 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3818 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_last_error_message(memc
));
3820 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3822 test_true(memcmp(value
, key
, 4) == 0);
3823 test_true(rc
== MEMCACHED_SUCCESS
);
3825 /* Test that we can turn it off */
3826 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3827 test_true(rc
== MEMCACHED_SUCCESS
);
3829 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3831 test_true(rc
== MEMCACHED_FAILURE
);
3833 /* Now setup for main test */
3834 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, (void *)key
);
3835 test_true(rc
== MEMCACHED_SUCCESS
);
3837 value
= (char *)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3839 test_true(rc
== MEMCACHED_SUCCESS
);
3840 test_true(memcmp(value
, key
, 4) == 0);
3842 /* Set to Zero, and then Set to something too large */
3845 memset(long_key
, 0, 255);
3847 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, NULL
);
3848 test_true(rc
== MEMCACHED_SUCCESS
);
3850 value
= (char*)memcached_callback_get(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, &rc
);
3852 test_true(rc
== MEMCACHED_FAILURE
);
3853 test_true(value
== NULL
);
3855 /* Test a long key for failure */
3856 /* TODO, extend test to determine based on setting, what result should be */
3857 strncpy(long_key
, "Thisismorethentheallottednumberofcharacters", sizeof(long_key
));
3858 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3859 //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED);
3860 test_true(rc
== MEMCACHED_SUCCESS
);
3862 /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
3863 strncpy(long_key
, "This is more then the allotted number of characters", sizeof(long_key
));
3864 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3865 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3867 /* Test for a bad prefix, but with a short key */
3868 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_VERIFY_KEY
, 1);
3869 test_true(rc
== MEMCACHED_SUCCESS
);
3871 strncpy(long_key
, "dog cat", sizeof(long_key
));
3872 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_PREFIX_KEY
, long_key
);
3873 test_true(rc
== MEMCACHED_BAD_KEY_PROVIDED
);
3876 return TEST_SUCCESS
;
3880 #ifdef MEMCACHED_ENABLE_DEPRECATED
3881 static test_return_t
deprecated_set_memory_alloc(memcached_st
*memc
)
3883 void *test_ptr
= NULL
;
3886 memcached_malloc_fn malloc_cb
=
3887 (memcached_malloc_fn
)my_malloc
;
3888 cb_ptr
= *(void **)&malloc_cb
;
3889 memcached_return_t rc
;
3891 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, cb_ptr
);
3892 test_true(rc
== MEMCACHED_SUCCESS
);
3893 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_MALLOC_FUNCTION
, &rc
);
3894 test_true(rc
== MEMCACHED_SUCCESS
);
3895 test_true(test_ptr
== cb_ptr
);
3899 memcached_realloc_fn realloc_cb
=
3900 (memcached_realloc_fn
)my_realloc
;
3901 cb_ptr
= *(void **)&realloc_cb
;
3902 memcached_return_t rc
;
3904 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, cb_ptr
);
3905 test_true(rc
== MEMCACHED_SUCCESS
);
3906 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_REALLOC_FUNCTION
, &rc
);
3907 test_true(rc
== MEMCACHED_SUCCESS
);
3908 test_true(test_ptr
== cb_ptr
);
3912 memcached_free_fn free_cb
=
3913 (memcached_free_fn
)my_free
;
3914 cb_ptr
= *(void **)&free_cb
;
3915 memcached_return_t rc
;
3917 rc
= memcached_callback_set(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, cb_ptr
);
3918 test_true(rc
== MEMCACHED_SUCCESS
);
3919 test_ptr
= memcached_callback_get(memc
, MEMCACHED_CALLBACK_FREE_FUNCTION
, &rc
);
3920 test_true(rc
== MEMCACHED_SUCCESS
);
3921 test_true(test_ptr
== cb_ptr
);
3924 return TEST_SUCCESS
;
3929 static test_return_t
set_memory_alloc(memcached_st
*memc
)
3931 memcached_return_t rc
;
3932 rc
= memcached_set_memory_allocators(memc
, NULL
, my_free
,
3933 my_realloc
, my_calloc
, NULL
);
3934 test_true(rc
== MEMCACHED_FAILURE
);
3936 rc
= memcached_set_memory_allocators(memc
, my_malloc
, my_free
,
3937 my_realloc
, my_calloc
, NULL
);
3939 memcached_malloc_fn mem_malloc
;
3940 memcached_free_fn mem_free
;
3941 memcached_realloc_fn mem_realloc
;
3942 memcached_calloc_fn mem_calloc
;
3943 memcached_get_memory_allocators(memc
, &mem_malloc
, &mem_free
,
3944 &mem_realloc
, &mem_calloc
);
3946 test_true(mem_malloc
== my_malloc
);
3947 test_true(mem_realloc
== my_realloc
);
3948 test_true(mem_calloc
== my_calloc
);
3949 test_true(mem_free
== my_free
);
3951 return TEST_SUCCESS
;
3954 static test_return_t
enable_consistent_crc(memcached_st
*memc
)
3957 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3958 memcached_hash_t hash
;
3959 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3960 if ((rc
= pre_crc(memc
)) != TEST_SUCCESS
)
3963 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3964 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3966 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3968 if (hash
!= MEMCACHED_HASH_CRC
)
3969 return TEST_SKIPPED
;
3971 return TEST_SUCCESS
;
3974 static test_return_t
enable_consistent_hsieh(memcached_st
*memc
)
3977 memcached_server_distribution_t value
= MEMCACHED_DISTRIBUTION_CONSISTENT
;
3978 memcached_hash_t hash
;
3979 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, value
);
3980 if ((rc
= pre_hsieh(memc
)) != TEST_SUCCESS
)
3983 value
= (memcached_server_distribution_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
);
3984 test_true(value
== MEMCACHED_DISTRIBUTION_CONSISTENT
);
3986 hash
= (memcached_hash_t
)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_HASH
);
3988 if (hash
!= MEMCACHED_HASH_HSIEH
)
3989 return TEST_SKIPPED
;
3992 return TEST_SUCCESS
;
3995 static test_return_t
enable_cas(memcached_st
*memc
)
3997 unsigned int set
= 1;
3999 if (libmemcached_util_version_check(memc
, 1, 2, 4))
4001 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, set
);
4003 return TEST_SUCCESS
;
4006 return TEST_SKIPPED
;
4009 static test_return_t
check_for_1_2_3(memcached_st
*memc
)
4011 memcached_version(memc
);
4013 memcached_server_instance_st instance
=
4014 memcached_server_instance_by_position(memc
, 0);
4016 if ((instance
->major_version
>= 1 && (instance
->minor_version
== 2 && instance
->micro_version
>= 4))
4017 || instance
->minor_version
> 2)
4019 return TEST_SUCCESS
;
4022 return TEST_SKIPPED
;
4025 static test_return_t
pre_unix_socket(memcached_st
*memc
)
4027 memcached_return_t rc
;
4030 memcached_servers_reset(memc
);
4032 if (stat("/tmp/memcached.socket", &buf
))
4033 return TEST_SKIPPED
;
4035 rc
= memcached_server_add_unix_socket_with_weight(memc
, "/tmp/memcached.socket", 0);
4037 return ( rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_FAILURE
);
4040 static test_return_t
pre_nodelay(memcached_st
*memc
)
4042 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
4043 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 0);
4045 return TEST_SUCCESS
;
4048 static test_return_t
pre_settimer(memcached_st
*memc
)
4050 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
4051 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
4053 return TEST_SUCCESS
;
4056 static test_return_t
poll_timeout(memcached_st
*memc
)
4062 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, timeout
);
4064 timeout
= (size_t)memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
);
4066 test_true(timeout
== 100);
4068 return TEST_SUCCESS
;
4071 static test_return_t
noreply_test(memcached_st
*memc
)
4073 memcached_return_t ret
;
4074 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
4075 test_true(ret
== MEMCACHED_SUCCESS
);
4076 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
4077 test_true(ret
== MEMCACHED_SUCCESS
);
4078 ret
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
, 1);
4079 test_true(ret
== MEMCACHED_SUCCESS
);
4080 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NOREPLY
) == 1);
4081 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
) == 1);
4082 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_SUPPORT_CAS
) == 1);
4084 for (int count
=0; count
< 5; ++count
)
4086 for (size_t x
= 0; x
< 100; ++x
)
4089 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4090 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4092 size_t len
= (size_t)check_length
;
4097 ret
= memcached_add(memc
, key
, len
, key
, len
, 0, 0);
4100 ret
= memcached_replace(memc
, key
, len
, key
, len
, 0, 0);
4103 ret
= memcached_set(memc
, key
, len
, key
, len
, 0, 0);
4106 ret
= memcached_append(memc
, key
, len
, key
, len
, 0, 0);
4109 ret
= memcached_prepend(memc
, key
, len
, key
, len
, 0, 0);
4115 test_true(ret
== MEMCACHED_SUCCESS
|| ret
== MEMCACHED_BUFFERED
);
4119 ** NOTE: Don't ever do this in your code! this is not a supported use of the
4120 ** API and is _ONLY_ done this way to verify that the library works the
4121 ** way it is supposed to do!!!!
4124 for (uint32_t x
= 0; x
< memcached_server_count(memc
); ++x
)
4126 memcached_server_instance_st instance
=
4127 memcached_server_instance_by_position(memc
, x
);
4128 no_msg
+=(int)(instance
->cursor_active
);
4131 test_true(no_msg
== 0);
4132 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4135 ** Now validate that all items was set properly!
4137 for (size_t x
= 0; x
< 100; ++x
)
4141 int check_length
= (size_t)snprintf(key
, sizeof(key
), "%lu", (unsigned long)x
);
4143 test_false((size_t)check_length
>= sizeof(key
) || check_length
< 0);
4145 size_t len
= (size_t)check_length
;
4148 char* value
=memcached_get(memc
, key
, strlen(key
),
4149 &length
, &flags
, &ret
);
4150 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4153 case 0: /* FALLTHROUGH */
4154 case 1: /* FALLTHROUGH */
4156 test_true(strncmp(value
, key
, len
) == 0);
4157 test_true(len
== length
);
4160 test_true(length
== len
* 2);
4163 test_true(length
== len
* 3);
4173 /* Try setting an illegal cas value (should not return an error to
4174 * the caller (because we don't expect a return message from the server)
4176 const char* keys
[]= {"0"};
4177 size_t lengths
[]= {1};
4180 memcached_result_st results_obj
;
4181 memcached_result_st
*results
;
4182 ret
= memcached_mget(memc
, keys
, lengths
, 1);
4183 test_true(ret
== MEMCACHED_SUCCESS
);
4185 results
= memcached_result_create(memc
, &results_obj
);
4187 results
= memcached_fetch_result(memc
, &results_obj
, &ret
);
4189 test_true(ret
== MEMCACHED_SUCCESS
);
4190 uint64_t cas
= memcached_result_cas(results
);
4191 memcached_result_free(&results_obj
);
4193 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4194 test_true(ret
== MEMCACHED_SUCCESS
);
4197 * The item will have a new cas value, so try to set it again with the old
4198 * value. This should fail!
4200 ret
= memcached_cas(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0, cas
);
4201 test_true(ret
== MEMCACHED_SUCCESS
);
4202 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
4203 char* value
=memcached_get(memc
, keys
[0], lengths
[0], &length
, &flags
, &ret
);
4204 test_true(ret
== MEMCACHED_SUCCESS
&& value
!= NULL
);
4207 return TEST_SUCCESS
;
4210 static test_return_t
analyzer_test(memcached_st
*memc
)
4212 memcached_return_t rc
;
4213 memcached_stat_st
*memc_stat
;
4214 memcached_analysis_st
*report
;
4216 memc_stat
= memcached_stat(memc
, NULL
, &rc
);
4217 test_true(rc
== MEMCACHED_SUCCESS
);
4218 test_true(memc_stat
);
4220 report
= memcached_analyze(memc
, memc_stat
, &rc
);
4221 test_true(rc
== MEMCACHED_SUCCESS
);
4225 memcached_stat_free(NULL
, memc_stat
);
4227 return TEST_SUCCESS
;
4230 /* Count the objects */
4231 static memcached_return_t
callback_dump_counter(const memcached_st
*ptr
,
4236 (void)ptr
; (void)key
; (void)key_length
;
4237 size_t *counter
= (size_t *)context
;
4239 *counter
= *counter
+ 1;
4241 return MEMCACHED_SUCCESS
;
4244 static test_return_t
dump_test(memcached_st
*memc
)
4247 memcached_dump_fn callbacks
[1];
4248 test_return_t main_rc
;
4250 callbacks
[0]= &callback_dump_counter
;
4252 /* No support for Binary protocol yet */
4253 if (memc
->flags
.binary_protocol
)
4254 return TEST_SUCCESS
;
4256 main_rc
= set_test3(memc
);
4258 test_true (main_rc
== TEST_SUCCESS
);
4260 memcached_return_t rc
= memcached_dump(memc
, callbacks
, (void *)&counter
, 1);
4261 test_true(rc
== MEMCACHED_SUCCESS
);
4263 /* We may have more then 32 if our previous flush has not completed */
4264 test_true(counter
>= 32);
4266 return TEST_SUCCESS
;
4269 #ifdef HAVE_LIBMEMCACHEDUTIL
4271 struct test_pool_context_st
{
4272 memcached_pool_st
* pool
;
4276 static void* connection_release(void *arg
)
4278 test_pool_context_st
*resource
= static_cast<test_pool_context_st
*>(arg
);
4281 // Release all of the memc we are holding
4282 assert(memcached_pool_push(resource
->pool
, resource
->mmc
) == MEMCACHED_SUCCESS
);
4286 #define POOL_SIZE 10
4287 static test_return_t
connection_pool_test(memcached_st
*memc
)
4289 memcached_pool_st
* pool
= memcached_pool_create(memc
, 5, POOL_SIZE
);
4290 test_true(pool
!= NULL
);
4291 memcached_st
*mmc
[POOL_SIZE
];
4292 memcached_return_t rc
;
4294 // Fill up our array that we will store the memc that are in the pool
4295 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4297 mmc
[x
]= memcached_pool_pop(pool
, false, &rc
);
4298 test_true(mmc
[x
] != NULL
);
4299 test_true(rc
== MEMCACHED_SUCCESS
);
4302 // All memc should be gone
4303 test_true(memcached_pool_pop(pool
, false, &rc
) == NULL
);
4304 test_true(rc
== MEMCACHED_SUCCESS
);
4307 test_pool_context_st item
= { pool
, mmc
[9] };
4309 pthread_create(&tid
, NULL
, connection_release
, &item
);
4310 mmc
[9]= memcached_pool_pop(pool
, true, &rc
);
4311 test_true(rc
== MEMCACHED_SUCCESS
);
4312 pthread_join(tid
, NULL
);
4314 const char *key
= "key";
4315 size_t keylen
= strlen(key
);
4317 // verify that I can do ops with all connections
4318 rc
= memcached_set(mmc
[0], key
, keylen
, "0", 1, 0, 0);
4319 test_true(rc
== MEMCACHED_SUCCESS
);
4321 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4323 uint64_t number_value
;
4324 rc
= memcached_increment(mmc
[x
], key
, keylen
, 1, &number_value
);
4325 test_true(rc
== MEMCACHED_SUCCESS
);
4326 test_true(number_value
== (x
+1));
4330 for (size_t x
= 0; x
< POOL_SIZE
; ++x
)
4332 test_true(memcached_pool_push(pool
, mmc
[x
]) == MEMCACHED_SUCCESS
);
4336 /* verify that I can set behaviors on the pool when I don't have all
4337 * of the connections in the pool. It should however be enabled
4338 * when I push the item into the pool
4340 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4341 test_true(mmc
[0] != NULL
);
4343 rc
= memcached_pool_behavior_set(pool
, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
, 9999);
4344 test_true(rc
== MEMCACHED_SUCCESS
);
4346 mmc
[1]= memcached_pool_pop(pool
, false, &rc
);
4347 test_true(mmc
[1] != NULL
);
4349 test_true(memcached_behavior_get(mmc
[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4350 test_true(memcached_pool_push(pool
, mmc
[1]) == MEMCACHED_SUCCESS
);
4351 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4353 mmc
[0]= memcached_pool_pop(pool
, false, &rc
);
4354 test_true(memcached_behavior_get(mmc
[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK
) == 9999);
4355 test_true(memcached_pool_push(pool
, mmc
[0]) == MEMCACHED_SUCCESS
);
4357 test_true(memcached_pool_destroy(pool
) == memc
);
4359 return TEST_SUCCESS
;
4362 static test_return_t
util_version_test(memcached_st
*memc
)
4366 if_successful
= libmemcached_util_version_check(memc
, 0, 0, 0);
4367 test_true(if_successful
== true);
4369 if_successful
= libmemcached_util_version_check(memc
, 9, 9, 9);
4371 // We expect failure
4374 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4375 fprintf(stderr
, "\nDumping Server Information\n\n");
4376 memcached_server_fn callbacks
[1];
4378 callbacks
[0]= dump_server_information
;
4379 memcached_server_cursor(memc
, callbacks
, (void *)stderr
, 1);
4380 fprintf(stderr
, "\n----------------------------------------------------------------------\n");
4382 test_true(if_successful
== false);
4384 memcached_server_instance_st instance
=
4385 memcached_server_instance_by_position(memc
, 0);
4387 memcached_version(memc
);
4389 // We only use one binary when we test, so this should be just fine.
4390 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, instance
->micro_version
);
4391 test_true(if_successful
== true);
4393 if (instance
->micro_version
> 0)
4394 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
-1));
4395 else if (instance
->minor_version
> 0)
4396 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
- 1), instance
->micro_version
);
4397 else if (instance
->major_version
> 0)
4398 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
-1), instance
->minor_version
, instance
->micro_version
);
4400 test_true(if_successful
== true);
4402 if (instance
->micro_version
> 0)
4403 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, instance
->minor_version
, (uint8_t)(instance
->micro_version
+1));
4404 else if (instance
->minor_version
> 0)
4405 if_successful
= libmemcached_util_version_check(memc
, instance
->major_version
, (uint8_t)(instance
->minor_version
+1), instance
->micro_version
);
4406 else if (instance
->major_version
> 0)
4407 if_successful
= libmemcached_util_version_check(memc
, (uint8_t)(instance
->major_version
+1), instance
->minor_version
, instance
->micro_version
);
4409 test_true(if_successful
== false);
4411 return TEST_SUCCESS
;
4414 static test_return_t
ping_test(memcached_st
*memc
)
4416 memcached_return_t rc
;
4417 memcached_server_instance_st instance
=
4418 memcached_server_instance_by_position(memc
, 0);
4420 // Test both the version that returns a code, and the one that does not.
4421 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4422 memcached_server_port(instance
), NULL
));
4424 test_true(libmemcached_util_ping(memcached_server_name(instance
),
4425 memcached_server_port(instance
), &rc
));
4427 test_true(rc
== MEMCACHED_SUCCESS
);
4429 return TEST_SUCCESS
;
4435 static test_return_t
hash_sanity_test (memcached_st
*memc
)
4439 assert(MEMCACHED_HASH_DEFAULT
== MEMCACHED_HASH_DEFAULT
);
4440 assert(MEMCACHED_HASH_MD5
== MEMCACHED_HASH_MD5
);
4441 assert(MEMCACHED_HASH_CRC
== MEMCACHED_HASH_CRC
);
4442 assert(MEMCACHED_HASH_FNV1_64
== MEMCACHED_HASH_FNV1_64
);
4443 assert(MEMCACHED_HASH_FNV1A_64
== MEMCACHED_HASH_FNV1A_64
);
4444 assert(MEMCACHED_HASH_FNV1_32
== MEMCACHED_HASH_FNV1_32
);
4445 assert(MEMCACHED_HASH_FNV1A_32
== MEMCACHED_HASH_FNV1A_32
);
4446 #ifdef HAVE_HSIEH_HASH
4447 assert(MEMCACHED_HASH_HSIEH
== MEMCACHED_HASH_HSIEH
);
4449 assert(MEMCACHED_HASH_MURMUR
== MEMCACHED_HASH_MURMUR
);
4450 assert(MEMCACHED_HASH_JENKINS
== MEMCACHED_HASH_JENKINS
);
4451 assert(MEMCACHED_HASH_MAX
== MEMCACHED_HASH_MAX
);
4453 return TEST_SUCCESS
;
4457 static test_return_t
hsieh_avaibility_test (memcached_st
*memc
)
4459 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4460 #ifdef HAVE_HSIEH_HASH
4461 expected_rc
= MEMCACHED_SUCCESS
;
4463 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4464 (uint64_t)MEMCACHED_HASH_HSIEH
);
4465 test_true(rc
== expected_rc
);
4467 return TEST_SUCCESS
;
4470 static test_return_t
murmur_avaibility_test (memcached_st
*memc
)
4472 memcached_return_t expected_rc
= MEMCACHED_INVALID_ARGUMENTS
;
4473 #ifdef HAVE_MURMUR_HASH
4474 expected_rc
= MEMCACHED_SUCCESS
;
4476 memcached_return_t rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
,
4477 (uint64_t)MEMCACHED_HASH_MURMUR
);
4478 test_true(rc
== expected_rc
);
4480 return TEST_SUCCESS
;
4483 static test_return_t
one_at_a_time_run (memcached_st
*memc
)
4489 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4493 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_DEFAULT
);
4494 test_true(one_at_a_time_values
[x
] == hash_val
);
4497 return TEST_SUCCESS
;
4500 static test_return_t
md5_run (memcached_st
*memc
)
4506 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4510 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MD5
);
4511 test_true(md5_values
[x
] == hash_val
);
4514 return TEST_SUCCESS
;
4517 static test_return_t
crc_run (memcached_st
*memc
)
4523 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4527 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_CRC
);
4528 test_true(crc_values
[x
] == hash_val
);
4531 return TEST_SUCCESS
;
4534 static test_return_t
fnv1_64_run (memcached_st
*memc
)
4540 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4544 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_64
);
4545 test_true(fnv1_64_values
[x
] == hash_val
);
4548 return TEST_SUCCESS
;
4551 static test_return_t
fnv1a_64_run (memcached_st
*memc
)
4557 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4561 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_64
);
4562 test_true(fnv1a_64_values
[x
] == hash_val
);
4565 return TEST_SUCCESS
;
4568 static test_return_t
fnv1_32_run (memcached_st
*memc
)
4574 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4578 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1_32
);
4579 test_true(fnv1_32_values
[x
] == hash_val
);
4582 return TEST_SUCCESS
;
4585 static test_return_t
fnv1a_32_run (memcached_st
*memc
)
4591 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4595 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_FNV1A_32
);
4596 test_true(fnv1a_32_values
[x
] == hash_val
);
4599 return TEST_SUCCESS
;
4602 static test_return_t
hsieh_run (memcached_st
*memc
)
4608 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4612 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_HSIEH
);
4613 test_true(hsieh_values
[x
] == hash_val
);
4616 return TEST_SUCCESS
;
4619 static test_return_t
murmur_run (memcached_st
*memc
)
4621 #ifdef WORDS_BIGENDIAN
4622 (void)murmur_values
;
4623 return TEST_SKIPPED
;
4629 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4633 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_MURMUR
);
4634 test_true(murmur_values
[x
] == hash_val
);
4637 return TEST_SUCCESS
;
4641 static test_return_t
jenkins_run (memcached_st
*memc
)
4647 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4651 hash_val
= memcached_generate_hash_value(*ptr
, strlen(*ptr
), MEMCACHED_HASH_JENKINS
);
4652 test_true(jenkins_values
[x
] == hash_val
);
4655 return TEST_SUCCESS
;
4658 static uint32_t hash_md5_test_function(const char *string
, size_t string_length
, void *context
)
4661 return libhashkit_md5(string
, string_length
);
4664 static uint32_t hash_crc_test_function(const char *string
, size_t string_length
, void *context
)
4667 return libhashkit_crc32(string
, string_length
);
4670 static test_return_t
memcached_get_hashkit_test (memcached_st
*memc
)
4674 const hashkit_st
*kit
;
4676 hashkit_return_t hash_rc
;
4678 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};
4679 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};
4681 kit
= memcached_get_hashkit(memc
);
4683 hashkit_clone(&new_kit
, kit
);
4684 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_md5_test_function
, NULL
);
4685 test_true(hash_rc
== HASHKIT_SUCCESS
);
4687 memcached_set_hashkit(memc
, &new_kit
);
4690 Verify Setting the hash.
4692 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4696 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4697 test_true(md5_values
[x
] == hash_val
);
4702 Now check memcached_st.
4704 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4708 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4709 test_true(md5_hosts
[x
] == hash_val
);
4712 hash_rc
= hashkit_set_custom_function(&new_kit
, hash_crc_test_function
, NULL
);
4713 test_true(hash_rc
== HASHKIT_SUCCESS
);
4715 memcached_set_hashkit(memc
, &new_kit
);
4718 Verify Setting the hash.
4720 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4724 hash_val
= hashkit_digest(kit
, *ptr
, strlen(*ptr
));
4725 test_true(crc_values
[x
] == hash_val
);
4728 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
4732 hash_val
= memcached_generate_hash(memc
, *ptr
, strlen(*ptr
));
4733 test_true(crc_hosts
[x
] == hash_val
);
4736 return TEST_SUCCESS
;
4740 Test case adapted from John Gorman <johngorman2@gmail.com>
4742 We are testing the error condition when we connect to a server via memcached_get()
4743 but find that the server is not available.
4745 static test_return_t
memcached_get_MEMCACHED_ERRNO(memcached_st
*memc
)
4748 memcached_st
*tl_memc_h
;
4749 memcached_server_st
*servers
;
4751 const char *key
= "MemcachedLives";
4754 memcached_return rc
;
4758 tl_memc_h
= memcached_create(NULL
);
4759 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4760 memcached_server_push(tl_memc_h
, servers
);
4761 memcached_server_list_free(servers
);
4763 // See if memcached is reachable.
4764 value
= memcached_get(tl_memc_h
, key
, strlen(key
), &len
, &flags
, &rc
);
4767 test_true(len
== 0);
4768 test_false(rc
== MEMCACHED_SUCCESS
);
4770 memcached_free(tl_memc_h
);
4772 return TEST_SUCCESS
;
4776 We connect to a server which exists, but search for a key that does not exist.
4778 static test_return_t
memcached_get_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4780 const char *key
= "MemcachedKeyNotEXIST";
4783 memcached_return rc
;
4786 // See if memcached is reachable.
4787 value
= memcached_get(memc
, key
, strlen(key
), &len
, &flags
, &rc
);
4790 test_true(len
== 0);
4791 test_true(rc
== MEMCACHED_NOTFOUND
);
4793 return TEST_SUCCESS
;
4797 Test case adapted from John Gorman <johngorman2@gmail.com>
4799 We are testing the error condition when we connect to a server via memcached_get_by_key()
4800 but find that the server is not available.
4802 static test_return_t
memcached_get_by_key_MEMCACHED_ERRNO(memcached_st
*memc
)
4805 memcached_st
*tl_memc_h
;
4806 memcached_server_st
*servers
;
4808 const char *key
= "MemcachedLives";
4811 memcached_return rc
;
4815 tl_memc_h
= memcached_create(NULL
);
4816 servers
= memcached_servers_parse("localhost:9898,localhost:9899"); // This server should not exist
4817 memcached_server_push(tl_memc_h
, servers
);
4818 memcached_server_list_free(servers
);
4820 // See if memcached is reachable.
4821 value
= memcached_get_by_key(tl_memc_h
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4824 test_true(len
== 0);
4825 test_false(rc
== MEMCACHED_SUCCESS
);
4827 memcached_free(tl_memc_h
);
4829 return TEST_SUCCESS
;
4833 We connect to a server which exists, but search for a key that does not exist.
4835 static test_return_t
memcached_get_by_key_MEMCACHED_NOTFOUND(memcached_st
*memc
)
4837 const char *key
= "MemcachedKeyNotEXIST";
4840 memcached_return rc
;
4843 // See if memcached is reachable.
4844 value
= memcached_get_by_key(memc
, key
, strlen(key
), key
, strlen(key
), &len
, &flags
, &rc
);
4847 test_true(len
== 0);
4848 test_true(rc
== MEMCACHED_NOTFOUND
);
4850 return TEST_SUCCESS
;
4854 static test_return_t
ketama_compatibility_libmemcached(memcached_st
*trash
)
4856 memcached_return_t rc
;
4859 memcached_server_st
*server_pool
;
4864 memc
= memcached_create(NULL
);
4867 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4868 test_true(rc
== MEMCACHED_SUCCESS
);
4870 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4871 test_true(value
== 1);
4873 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
) == MEMCACHED_SUCCESS
);
4874 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
);
4877 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");
4878 memcached_server_push(memc
, server_pool
);
4880 /* verify that the server list was parsed okay. */
4881 test_true(memcached_server_count(memc
) == 8);
4882 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4883 test_true(server_pool
[0].port
== 11211);
4884 test_true(server_pool
[0].weight
== 600);
4885 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4886 test_true(server_pool
[2].port
== 11211);
4887 test_true(server_pool
[2].weight
== 200);
4888 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4889 test_true(server_pool
[7].port
== 11211);
4890 test_true(server_pool
[7].weight
== 100);
4892 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4893 * us test the boundary wraparound.
4895 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4897 /* verify the standard ketama set. */
4898 for (x
= 0; x
< 99; x
++)
4900 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases
[x
].key
, strlen(ketama_test_cases
[x
].key
));
4901 memcached_server_instance_st instance
=
4902 memcached_server_instance_by_position(memc
, server_idx
);
4903 const char *hostname
= memcached_server_name(instance
);
4905 test_strcmp(hostname
, ketama_test_cases
[x
].server
);
4908 memcached_server_list_free(server_pool
);
4909 memcached_free(memc
);
4911 return TEST_SUCCESS
;
4914 static test_return_t
ketama_compatibility_spymemcached(memcached_st
*trash
)
4916 memcached_return_t rc
;
4918 memcached_server_st
*server_pool
;
4923 memc
= memcached_create(NULL
);
4926 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
, 1);
4927 test_true(rc
== MEMCACHED_SUCCESS
);
4929 value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
4930 test_true(value
== 1);
4932 test_true(memcached_behavior_set_distribution(memc
, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
) == MEMCACHED_SUCCESS
);
4933 test_true(memcached_behavior_get_distribution(memc
) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
);
4935 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");
4936 assert(server_pool
);
4937 memcached_server_push(memc
, server_pool
);
4939 /* verify that the server list was parsed okay. */
4940 test_true(memcached_server_count(memc
) == 8);
4941 test_strcmp(server_pool
[0].hostname
, "10.0.1.1");
4942 test_true(server_pool
[0].port
== 11211);
4943 test_true(server_pool
[0].weight
== 600);
4944 test_strcmp(server_pool
[2].hostname
, "10.0.1.3");
4945 test_true(server_pool
[2].port
== 11211);
4946 test_true(server_pool
[2].weight
== 200);
4947 test_strcmp(server_pool
[7].hostname
, "10.0.1.8");
4948 test_true(server_pool
[7].port
== 11211);
4949 test_true(server_pool
[7].weight
== 100);
4951 /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
4952 * us test the boundary wraparound.
4954 test_true(memcached_generate_hash(memc
, (char *)"VDEAAAAA", 8) == memc
->ketama
.continuum
[0].index
);
4956 /* verify the standard ketama set. */
4957 for (uint32_t x
= 0; x
< 99; x
++)
4959 uint32_t server_idx
= memcached_generate_hash(memc
, ketama_test_cases_spy
[x
].key
, strlen(ketama_test_cases_spy
[x
].key
));
4961 memcached_server_instance_st instance
=
4962 memcached_server_instance_by_position(memc
, server_idx
);
4964 const char *hostname
= memcached_server_name(instance
);
4966 test_strcmp(hostname
, ketama_test_cases_spy
[x
].server
);
4969 memcached_server_list_free(server_pool
);
4970 memcached_free(memc
);
4972 return TEST_SUCCESS
;
4975 static test_return_t
regression_bug_434484(memcached_st
*memc
)
4977 test_return_t test_rc
;
4978 test_rc
= pre_binary(memc
);
4980 if (test_rc
!= TEST_SUCCESS
)
4983 const char *key
= "regression_bug_434484";
4984 size_t keylen
= strlen(key
);
4986 memcached_return_t ret
= memcached_append(memc
, key
, keylen
, key
, keylen
, 0, 0);
4987 test_true(ret
== MEMCACHED_NOTSTORED
);
4989 size_t size
= 2048 * 1024;
4990 char *data
= (char*)calloc(1, size
);
4992 ret
= memcached_set(memc
, key
, keylen
, data
, size
, 0, 0);
4993 test_true(ret
== MEMCACHED_E2BIG
);
4996 return TEST_SUCCESS
;
4999 static test_return_t
regression_bug_434843(memcached_st
*memc
)
5001 test_return_t test_rc
;
5002 test_rc
= pre_binary(memc
);
5004 if (test_rc
!= TEST_SUCCESS
)
5007 memcached_return_t rc
;
5009 memcached_execute_fn callbacks
[]= { &callback_counter
};
5012 * I only want to hit only _one_ server so I know the number of requests I'm
5013 * sending in the pipleine to the server. Let's try to do a multiget of
5014 * 1024 (that should satisfy most users don't you think?). Future versions
5015 * will include a mget_execute function call if you need a higher number.
5017 uint32_t number_of_hosts
= memcached_server_count(memc
);
5018 memc
->number_of_hosts
= 1;
5019 const size_t max_keys
= 1024;
5020 char **keys
= (char**)calloc(max_keys
, sizeof(char*));
5021 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5023 for (size_t x
= 0; x
< max_keys
; ++x
)
5027 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5029 test_true(keys
[x
] != NULL
);
5033 * Run two times.. the first time we should have 100% cache miss,
5034 * and the second time we should have 100% cache hits
5036 for (size_t y
= 0; y
< 2; y
++)
5038 rc
= memcached_mget(memc
, (const char**)keys
, key_length
, max_keys
);
5039 test_true(rc
== MEMCACHED_SUCCESS
);
5040 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5044 /* The first iteration should give me a 100% cache miss. verify that*/
5045 char blob
[1024]= { 0 };
5047 test_true(counter
== 0);
5049 for (size_t x
= 0; x
< max_keys
; ++x
)
5051 rc
= memcached_add(memc
, keys
[x
], key_length
[x
],
5052 blob
, sizeof(blob
), 0, 0);
5053 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5058 /* Verify that we received all of the key/value pairs */
5059 test_true(counter
== max_keys
);
5063 /* Release allocated resources */
5064 for (size_t x
= 0; x
< max_keys
; ++x
)
5071 memc
->number_of_hosts
= number_of_hosts
;
5073 return TEST_SUCCESS
;
5076 static test_return_t
regression_bug_434843_buffered(memcached_st
*memc
)
5078 memcached_return_t rc
;
5079 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
5080 test_true(rc
== MEMCACHED_SUCCESS
);
5082 return regression_bug_434843(memc
);
5085 static test_return_t
regression_bug_421108(memcached_st
*memc
)
5087 memcached_return_t rc
;
5088 memcached_stat_st
*memc_stat
= memcached_stat(memc
, NULL
, &rc
);
5089 test_true(rc
== MEMCACHED_SUCCESS
);
5091 char *bytes
= memcached_stat_get_value(memc
, memc_stat
, "bytes", &rc
);
5092 test_true(rc
== MEMCACHED_SUCCESS
);
5093 test_true(bytes
!= NULL
);
5094 char *bytes_read
= memcached_stat_get_value(memc
, memc_stat
,
5096 test_true(rc
== MEMCACHED_SUCCESS
);
5097 test_true(bytes_read
!= NULL
);
5099 char *bytes_written
= memcached_stat_get_value(memc
, memc_stat
,
5100 "bytes_written", &rc
);
5101 test_true(rc
== MEMCACHED_SUCCESS
);
5102 test_true(bytes_written
!= NULL
);
5104 test_true(strcmp(bytes
, bytes_read
) != 0);
5105 test_true(strcmp(bytes
, bytes_written
) != 0);
5107 /* Release allocated resources */
5110 free(bytes_written
);
5111 memcached_stat_free(NULL
, memc_stat
);
5113 return TEST_SUCCESS
;
5117 * The test case isn't obvious so I should probably document why
5118 * it works the way it does. Bug 442914 was caused by a bug
5119 * in the logic in memcached_purge (it did not handle the case
5120 * where the number of bytes sent was equal to the watermark).
5121 * In this test case, create messages so that we hit that case
5122 * and then disable noreply mode and issue a new command to
5123 * verify that it isn't stuck. If we change the format for the
5124 * delete command or the watermarks, we need to update this
5127 static test_return_t
regression_bug_442914(memcached_st
*memc
)
5129 memcached_return_t rc
;
5130 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1);
5131 test_true(rc
== MEMCACHED_SUCCESS
);
5132 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5134 uint32_t number_of_hosts
= memcached_server_count(memc
);
5135 memc
->number_of_hosts
= 1;
5140 for (uint32_t x
= 0; x
< 250; ++x
)
5142 len
= (size_t)snprintf(k
, sizeof(k
), "%0250u", x
);
5143 rc
= memcached_delete(memc
, k
, len
, 0);
5144 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5147 (void)snprintf(k
, sizeof(k
), "%037u", 251U);
5150 rc
= memcached_delete(memc
, k
, len
, 0);
5151 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5153 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0);
5154 test_true(rc
== MEMCACHED_SUCCESS
);
5155 rc
= memcached_delete(memc
, k
, len
, 0);
5156 test_true(rc
== MEMCACHED_NOTFOUND
);
5158 memc
->number_of_hosts
= number_of_hosts
;
5160 return TEST_SUCCESS
;
5163 static test_return_t
regression_bug_447342(memcached_st
*memc
)
5165 memcached_server_instance_st instance_one
;
5166 memcached_server_instance_st instance_two
;
5168 if (memcached_server_count(memc
) < 3 || pre_replication(memc
) != TEST_SUCCESS
)
5169 return TEST_SKIPPED
;
5171 memcached_return_t rc
;
5173 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS
, 2);
5174 test_true(rc
== MEMCACHED_SUCCESS
);
5176 const size_t max_keys
= 100;
5177 char **keys
= (char**)calloc(max_keys
, sizeof(char*));
5178 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5180 for (size_t x
= 0; x
< max_keys
; ++x
)
5184 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5186 test_true(keys
[x
] != NULL
);
5187 rc
= memcached_set(memc
, k
, key_length
[x
], k
, key_length
[x
], 0, 0);
5188 test_true(rc
== MEMCACHED_SUCCESS
);
5192 ** We are using the quiet commands to store the replicas, so we need
5193 ** to ensure that all of them are processed before we can continue.
5194 ** In the test we go directly from storing the object to trying to
5195 ** receive the object from all of the different servers, so we
5196 ** could end up in a race condition (the memcached server hasn't yet
5197 ** processed the quiet command from the replication set when it process
5198 ** the request from the other client (created by the clone)). As a
5199 ** workaround for that we call memcached_quit to send the quit command
5200 ** to the server and wait for the response ;-) If you use the test code
5201 ** as an example for your own code, please note that you shouldn't need
5204 memcached_quit(memc
);
5206 /* Verify that all messages are stored, and we didn't stuff too much
5209 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5210 test_true(rc
== MEMCACHED_SUCCESS
);
5213 memcached_execute_fn callbacks
[]= { &callback_counter
};
5214 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5215 /* Verify that we received all of the key/value pairs */
5216 test_true(counter
== max_keys
);
5218 memcached_quit(memc
);
5220 * Don't do the following in your code. I am abusing the internal details
5221 * within the library, and this is not a supported interface.
5222 * This is to verify correct behavior in the library. Fake that two servers
5225 instance_one
= memcached_server_instance_by_position(memc
, 0);
5226 instance_two
= memcached_server_instance_by_position(memc
, 2);
5227 in_port_t port0
= instance_one
->port
;
5228 in_port_t port2
= instance_two
->port
;
5230 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5231 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5233 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5234 test_true(rc
== MEMCACHED_SUCCESS
);
5237 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5238 test_true(counter
== (unsigned int)max_keys
);
5240 /* restore the memc handle */
5241 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5242 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5244 memcached_quit(memc
);
5246 /* Remove half of the objects */
5247 for (size_t x
= 0; x
< max_keys
; ++x
)
5251 rc
= memcached_delete(memc
, keys
[x
], key_length
[x
], 0);
5252 test_true(rc
== MEMCACHED_SUCCESS
);
5256 memcached_quit(memc
);
5257 ((memcached_server_write_instance_st
)instance_one
)->port
= 0;
5258 ((memcached_server_write_instance_st
)instance_two
)->port
= 0;
5260 /* now retry the command, this time we should have cache misses */
5261 rc
= memcached_mget(memc
, (const char* const *)keys
, key_length
, max_keys
);
5262 test_true(rc
== MEMCACHED_SUCCESS
);
5265 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&counter
, 1);
5266 test_true(counter
== (unsigned int)(max_keys
>> 1));
5268 /* Release allocated resources */
5269 for (size_t x
= 0; x
< max_keys
; ++x
)
5276 /* restore the memc handle */
5277 ((memcached_server_write_instance_st
)instance_one
)->port
= port0
;
5278 ((memcached_server_write_instance_st
)instance_two
)->port
= port2
;
5280 return TEST_SUCCESS
;
5283 static test_return_t
regression_bug_463297(memcached_st
*memc
)
5285 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
5286 test_true(memc_clone
!= NULL
);
5287 test_true(memcached_version(memc_clone
) == MEMCACHED_SUCCESS
);
5289 memcached_server_instance_st instance
=
5290 memcached_server_instance_by_position(memc_clone
, 0);
5292 if (instance
->major_version
> 1 ||
5293 (instance
->major_version
== 1 &&
5294 instance
->minor_version
> 2))
5296 /* Binary protocol doesn't support deferred delete */
5297 memcached_st
*bin_clone
= memcached_clone(NULL
, memc
);
5298 test_true(bin_clone
!= NULL
);
5299 test_true(memcached_behavior_set(bin_clone
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1) == MEMCACHED_SUCCESS
);
5300 test_true(memcached_delete(bin_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5301 memcached_free(bin_clone
);
5303 memcached_quit(memc_clone
);
5305 /* If we know the server version, deferred delete should fail
5306 * with invalid arguments */
5307 test_true(memcached_delete(memc_clone
, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS
);
5309 /* If we don't know the server version, we should get a protocol error */
5310 memcached_return_t rc
= memcached_delete(memc
, "foo", 3, 1);
5312 /* but there is a bug in some of the memcached servers (1.4) that treats
5313 * the counter as noreply so it doesn't send the proper error message
5315 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5317 /* And buffered mode should be disabled and we should get protocol error */
5318 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1) == MEMCACHED_SUCCESS
);
5319 rc
= memcached_delete(memc
, "foo", 3, 1);
5320 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5322 /* Same goes for noreply... */
5323 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 1) == MEMCACHED_SUCCESS
);
5324 rc
= memcached_delete(memc
, "foo", 3, 1);
5325 test_true_got(rc
== MEMCACHED_PROTOCOL_ERROR
|| rc
== MEMCACHED_NOTFOUND
|| rc
== MEMCACHED_CLIENT_ERROR
|| rc
== MEMCACHED_INVALID_ARGUMENTS
, memcached_strerror(NULL
, rc
));
5327 /* but a normal request should go through (and be buffered) */
5328 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_BUFFERED
);
5329 test_true(memcached_flush_buffers(memc
) == MEMCACHED_SUCCESS
);
5331 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0) == MEMCACHED_SUCCESS
);
5332 /* unbuffered noreply should be success */
5333 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_SUCCESS
);
5334 /* unbuffered with reply should be not found... */
5335 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, 0) == MEMCACHED_SUCCESS
);
5336 test_true(memcached_delete(memc
, "foo", 3, 0) == MEMCACHED_NOTFOUND
);
5339 memcached_free(memc_clone
);
5340 return TEST_SUCCESS
;
5344 /* Test memcached_server_get_last_disconnect
5345 * For a working server set, shall be NULL
5346 * For a set of non existing server, shall not be NULL
5348 static test_return_t
test_get_last_disconnect(memcached_st
*memc
)
5350 memcached_return_t rc
;
5351 memcached_server_instance_st disconnected_server
;
5353 /* With the working set of server */
5354 const char *key
= "marmotte";
5355 const char *value
= "milka";
5357 memcached_reset_last_disconnected_server(memc
);
5358 rc
= memcached_set(memc
, key
, strlen(key
),
5359 value
, strlen(value
),
5360 (time_t)0, (uint32_t)0);
5361 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5363 disconnected_server
= memcached_server_get_last_disconnect(memc
);
5364 test_true(disconnected_server
== NULL
);
5366 /* With a non existing server */
5368 memcached_server_st
*servers
;
5370 const char *server_list
= "localhost:9";
5372 servers
= memcached_servers_parse(server_list
);
5374 mine
= memcached_create(NULL
);
5375 rc
= memcached_server_push(mine
, servers
);
5376 test_true(rc
== MEMCACHED_SUCCESS
);
5377 memcached_server_list_free(servers
);
5380 rc
= memcached_set(mine
, key
, strlen(key
),
5381 value
, strlen(value
),
5382 (time_t)0, (uint32_t)0);
5383 test_true(rc
!= MEMCACHED_SUCCESS
);
5385 disconnected_server
= memcached_server_get_last_disconnect(mine
);
5386 if (disconnected_server
== NULL
)
5388 fprintf(stderr
, "RC %s\n", memcached_strerror(mine
, rc
));
5391 test_true(disconnected_server
!= NULL
);
5392 test_true(memcached_server_port(disconnected_server
)== 9);
5393 test_true(strncmp(memcached_server_name(disconnected_server
),"localhost",9) == 0);
5395 memcached_quit(mine
);
5396 memcached_free(mine
);
5398 return TEST_SUCCESS
;
5401 static test_return_t
test_verbosity(memcached_st
*memc
)
5403 memcached_verbosity(memc
, 3);
5405 return TEST_SUCCESS
;
5408 static test_return_t
test_server_failure(memcached_st
*memc
)
5410 memcached_st
*local_memc
;
5411 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, 0);
5413 local_memc
= memcached_create(NULL
);
5415 memcached_server_add(local_memc
, memcached_server_name(instance
), memcached_server_port(instance
));
5416 memcached_behavior_set(local_memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 2);
5418 uint32_t server_count
= memcached_server_count(local_memc
);
5420 test_true(server_count
== 1);
5422 // Disable the server
5423 instance
= memcached_server_instance_by_position(local_memc
, 0);
5424 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 2;
5426 memcached_return_t rc
;
5427 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5429 (time_t)0, (uint32_t)0);
5430 test_true(rc
== MEMCACHED_SERVER_MARKED_DEAD
);
5432 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5433 rc
= memcached_set(local_memc
, "foo", strlen("foo"),
5435 (time_t)0, (uint32_t)0);
5436 test_true(rc
== MEMCACHED_SUCCESS
);
5439 memcached_free(local_memc
);
5441 return TEST_SUCCESS
;
5444 static test_return_t
test_cull_servers(memcached_st
*memc
)
5446 uint32_t count
= memcached_server_count(memc
);
5448 // Do not do this in your code, it is not supported.
5449 memc
->servers
[1].options
.is_dead
= true;
5450 memc
->state
.is_time_for_rebuild
= true;
5452 uint32_t new_count
= memcached_server_count(memc
);
5453 test_true(count
== new_count
);
5456 test_true(count
== new_count
+ 1 );
5459 return TEST_SUCCESS
;
5463 static memcached_return_t
stat_printer(memcached_server_instance_st server
,
5464 const char *key
, size_t key_length
,
5465 const char *value
, size_t value_length
,
5475 return MEMCACHED_SUCCESS
;
5478 static test_return_t
memcached_stat_execute_test(memcached_st
*memc
)
5480 memcached_return_t rc
= memcached_stat_execute(memc
, NULL
, stat_printer
, NULL
);
5481 test_true(rc
== MEMCACHED_SUCCESS
);
5483 rc
= memcached_stat_execute(memc
, "slabs", stat_printer
, NULL
);
5484 test_true(rc
== MEMCACHED_SUCCESS
);
5486 rc
= memcached_stat_execute(memc
, "items", stat_printer
, NULL
);
5487 test_true(rc
== MEMCACHED_SUCCESS
);
5489 rc
= memcached_stat_execute(memc
, "sizes", stat_printer
, NULL
);
5490 test_true(rc
== MEMCACHED_SUCCESS
);
5492 return TEST_SUCCESS
;
5496 * This test ensures that the failure counter isn't incremented during
5497 * normal termination of the memcached instance.
5499 static test_return_t
wrong_failure_counter_test(memcached_st
*memc
)
5501 memcached_return_t rc
;
5502 memcached_server_instance_st instance
;
5504 /* Set value to force connection to the server */
5505 const char *key
= "marmotte";
5506 const char *value
= "milka";
5509 * Please note that I'm abusing the internal structures in libmemcached
5510 * in a non-portable way and you shouldn't be doing this. I'm only
5511 * doing this in order to verify that the library works the way it should
5513 uint32_t number_of_hosts
= memcached_server_count(memc
);
5514 memc
->number_of_hosts
= 1;
5516 /* Ensure that we are connected to the server by setting a value */
5517 rc
= memcached_set(memc
, key
, strlen(key
),
5518 value
, strlen(value
),
5519 (time_t)0, (uint32_t)0);
5520 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
5523 instance
= memcached_server_instance_by_position(memc
, 0);
5524 /* The test is to see that the memcached_quit doesn't increase the
5525 * the server failure conter, so let's ensure that it is zero
5526 * before sending quit
5528 ((memcached_server_write_instance_st
)instance
)->server_failure_counter
= 0;
5530 memcached_quit(memc
);
5532 /* Verify that it memcached_quit didn't increment the failure counter
5533 * Please note that this isn't bullet proof, because an error could
5536 test_true(instance
->server_failure_counter
== 0);
5538 /* restore the instance */
5539 memc
->number_of_hosts
= number_of_hosts
;
5541 return TEST_SUCCESS
;
5545 * This tests ensures expected disconnections (for some behavior changes
5546 * for instance) do not wrongly increase failure counter
5548 static test_return_t
wrong_failure_counter_two_test(memcached_st
*memc
)
5550 memcached_return rc
;
5552 memcached_st
*memc_clone
;
5553 memc_clone
= memcached_clone(NULL
, memc
);
5554 test_true(memc_clone
);
5556 /* Set value to force connection to the server */
5557 const char *key
= "marmotte";
5558 const char *value
= "milka";
5559 char *string
= NULL
;
5560 size_t string_length
;
5563 rc
= memcached_set(memc_clone
, key
, strlen(key
),
5564 value
, strlen(value
),
5565 (time_t)0, (uint32_t)0);
5566 test_true_got(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
5569 /* put failure limit to 1 */
5570 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5571 assert(rc
== MEMCACHED_SUCCESS
);
5573 /* Put a retry timeout to effectively activate failure_limit effect */
5574 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1);
5575 assert(rc
== MEMCACHED_SUCCESS
);
5577 /* change behavior that triggers memcached_quit()*/
5578 rc
= memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
5579 assert(rc
== MEMCACHED_SUCCESS
);
5582 /* Check if we still are connected */
5583 string
= memcached_get(memc_clone
, key
, strlen(key
),
5584 &string_length
, &flags
, &rc
);
5586 test_true_got(rc
== MEMCACHED_SUCCESS
, memcached_strerror(NULL
, rc
));
5589 memcached_free(memc_clone
);
5591 return TEST_SUCCESS
;
5598 * Test that ensures mget_execute does not end into recursive calls that finally fails
5600 static test_return_t
regression_bug_490486(memcached_st
*memc
)
5602 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5603 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
5604 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5605 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 1);
5606 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 3600);
5609 return TEST_SKIPPED
; // My MAC can't handle this test
5613 * I only want to hit _one_ server so I know the number of requests I'm
5614 * sending in the pipeline.
5616 uint32_t number_of_hosts
= memc
->number_of_hosts
;
5617 memc
->number_of_hosts
= 1;
5618 size_t max_keys
= 20480;
5621 char **keys
= (char **)calloc(max_keys
, sizeof(char*));
5622 size_t *key_length
= (size_t *)calloc(max_keys
, sizeof(size_t));
5624 /* First add all of the items.. */
5626 char blob
[1024]= { 0 };
5627 memcached_return rc
;
5628 for (size_t x
= 0; x
< max_keys
; ++x
)
5631 key_length
[x
]= (size_t)snprintf(k
, sizeof(k
), "0200%lu", (unsigned long)x
);
5633 assert(keys
[x
] != NULL
);
5634 rc
= memcached_set(memc
, keys
[x
], key_length
[x
], blob
, sizeof(blob
), 0, 0);
5636 if (rc
== MEMCACHED_SERVER_MARKED_DEAD
)
5638 break; // We are out of business
5641 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_TIMEOUT
); // MEMCACHED_TIMEOUT <-- only observed on OSX
5643 if (rc
== MEMCACHED_TIMEOUT
&& slept
== false)
5646 sleep(1);// We will try to sleep
5649 else if (rc
== MEMCACHED_TIMEOUT
&& slept
== true)
5651 // We failed to send everything.
5656 if (rc
!= MEMCACHED_SERVER_MARKED_DEAD
)
5659 /* Try to get all of them with a large multiget */
5661 memcached_execute_function callbacks
[]= { &callback_counter
};
5662 rc
= memcached_mget_execute(memc
, (const char**)keys
, key_length
,
5663 (size_t)max_keys
, callbacks
, &counter
, 1);
5665 assert(rc
== MEMCACHED_SUCCESS
);
5666 char* the_value
= NULL
;
5667 char the_key
[MEMCACHED_MAX_KEY
];
5668 size_t the_key_length
;
5669 size_t the_value_length
;
5673 the_value
= memcached_fetch(memc
, the_key
, &the_key_length
, &the_value_length
, &the_flags
, &rc
);
5675 if ((the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
))
5681 } while ( (the_value
!= NULL
) && (rc
== MEMCACHED_SUCCESS
));
5684 assert(rc
== MEMCACHED_END
);
5686 /* Verify that we got all of the items */
5687 assert(counter
== max_keys
);
5690 /* Release all allocated resources */
5691 for (size_t x
= 0; x
< max_keys
; ++x
)
5698 memc
->number_of_hosts
= number_of_hosts
;
5700 return TEST_SUCCESS
;
5703 static test_return_t
regression_bug_583031(memcached_st
*)
5705 memcached_st
*memc
= memcached_create(NULL
);
5707 test_true(memcached_success(memcached_server_add(memc
, "10.2.3.4", 11211)));
5709 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT
, 1000);
5710 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 1000);
5711 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SND_TIMEOUT
, 1000);
5712 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RCV_TIMEOUT
, 1000);
5713 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_POLL_TIMEOUT
, 1000);
5714 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT
, 3);
5716 memcached_return_t rc
;
5720 const char *value
= memcached_get(memc
, "dsf", 3, &length
, &flags
, &rc
);
5722 test_compare(0, length
);
5724 test_true_got(rc
== MEMCACHED_TIMEOUT
|| rc
== MEMCACHED_ERRNO
|| rc
== MEMCACHED_FAILURE
, memcached_strerror(memc
, rc
));
5726 memcached_free(memc
);
5728 return TEST_SUCCESS
;
5731 static test_return_t
regression_bug_581030(memcached_st
*)
5734 memcached_stat_st
*local_stat
= memcached_stat(NULL
, NULL
, NULL
);
5735 test_false(local_stat
);
5737 memcached_stat_free(NULL
, NULL
);
5740 return TEST_SUCCESS
;
5743 static void memcached_die(memcached_st
* mc
, memcached_return error
, const char* what
, uint32_t it
)
5745 fprintf(stderr
, "Iteration #%u: ", it
);
5747 if(error
== MEMCACHED_ERRNO
)
5749 fprintf(stderr
, "system error %d from %s: %s\n",
5750 errno
, what
, strerror(errno
));
5754 fprintf(stderr
, "error %d from %s: %s\n", error
, what
,
5755 memcached_strerror(mc
, error
));
5759 #define TEST_CONSTANT_CREATION 200
5761 static test_return_t
regression_bug_(memcached_st
*memc
)
5763 const char *remote_server
;
5766 if (! (remote_server
= getenv("LIBMEMCACHED_REMOTE_SERVER")))
5768 return TEST_SKIPPED
;
5771 for (uint32_t x
= 0; x
< TEST_CONSTANT_CREATION
; x
++)
5773 memcached_st
* mc
= memcached_create(NULL
);
5774 memcached_return rc
;
5776 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
5777 if (rc
!= MEMCACHED_SUCCESS
)
5779 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5782 rc
= memcached_behavior_set(mc
, MEMCACHED_BEHAVIOR_CACHE_LOOKUPS
, 1);
5783 if (rc
!= MEMCACHED_SUCCESS
)
5785 memcached_die(mc
, rc
, "memcached_behavior_set", x
);
5788 rc
= memcached_server_add(mc
, remote_server
, 0);
5789 if (rc
!= MEMCACHED_SUCCESS
)
5791 memcached_die(mc
, rc
, "memcached_server_add", x
);
5794 const char *set_key
= "akey";
5795 const size_t set_key_len
= strlen(set_key
);
5796 const char *set_value
= "a value";
5797 const size_t set_value_len
= strlen(set_value
);
5799 if (rc
== MEMCACHED_SUCCESS
)
5803 size_t get_value_len
;
5805 uint32_t get_value_flags
;
5807 get_value
= memcached_get(mc
, set_key
, set_key_len
, &get_value_len
,
5808 &get_value_flags
, &rc
);
5809 if (rc
!= MEMCACHED_SUCCESS
)
5811 memcached_die(mc
, rc
, "memcached_get", x
);
5817 (get_value_len
!= set_value_len
5818 || 0!=strncmp(get_value
, set_value
, get_value_len
)))
5820 fprintf(stderr
, "Values don't match?\n");
5821 rc
= MEMCACHED_FAILURE
;
5827 rc
= memcached_set(mc
,
5828 set_key
, set_key_len
,
5829 set_value
, set_value_len
,
5833 if (rc
!= MEMCACHED_SUCCESS
)
5835 memcached_die(mc
, rc
, "memcached_set", x
);
5842 if (rc
!= MEMCACHED_SUCCESS
)
5848 return TEST_SUCCESS
;
5852 * Test that the sasl authentication works. We cannot use the default
5853 * pool of servers, because that would require that all servers we want
5854 * to test supports SASL authentication, and that they use the default
5857 static test_return_t
sasl_auth_test(memcached_st
*memc
)
5859 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
5860 memcached_return_t rc
;
5862 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5863 test_true(rc
== MEMCACHED_SUCCESS
);
5864 test_true((rc
= memcached_delete(memc
, "foo", 3, 0)) == MEMCACHED_SUCCESS
);
5865 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_SUCCESS
);
5866 test_true((rc
= memcached_destroy_sasl_auth_data(memc
)) == MEMCACHED_FAILURE
);
5867 test_true((rc
= memcached_destroy_sasl_auth_data(NULL
)) == MEMCACHED_FAILURE
);
5868 memcached_quit(memc
);
5870 rc
= memcached_set_sasl_auth_data(memc
,
5871 getenv("LIBMEMCACHED_TEST_SASL_USERNAME"),
5872 getenv("LIBMEMCACHED_TEST_SASL_SERVER"));
5873 test_true(rc
== MEMCACHED_SUCCESS
);
5875 rc
= memcached_set(memc
, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0);
5876 test_true(rc
== MEMCACHED_AUTH_FAILURE
);
5877 test_true(memcached_destroy_sasl_auth_data(memc
) == MEMCACHED_SUCCESS
);
5879 memcached_quit(memc
);
5880 return TEST_SUCCESS
;
5883 return TEST_FAILURE
;
5887 /* Clean the server before beginning testing */
5889 {"util_version", 1, (test_callback_fn
)util_version_test
},
5890 {"flush", 0, (test_callback_fn
)flush_test
},
5891 {"init", 0, (test_callback_fn
)init_test
},
5892 {"allocation", 0, (test_callback_fn
)allocation_test
},
5893 {"server_list_null_test", 0, (test_callback_fn
)server_list_null_test
},
5894 {"server_unsort", 0, (test_callback_fn
)server_unsort_test
},
5895 {"server_sort", 0, (test_callback_fn
)server_sort_test
},
5896 {"server_sort2", 0, (test_callback_fn
)server_sort2_test
},
5897 {"memcached_server_remove", 0, (test_callback_fn
)memcached_server_remove_test
},
5898 {"clone_test", 0, (test_callback_fn
)clone_test
},
5899 {"connection_test", 0, (test_callback_fn
)connection_test
},
5900 {"callback_test", 0, (test_callback_fn
)callback_test
},
5901 {"userdata_test", 0, (test_callback_fn
)userdata_test
},
5902 {"error", 0, (test_callback_fn
)error_test
},
5903 {"set", 0, (test_callback_fn
)set_test
},
5904 {"set2", 0, (test_callback_fn
)set_test2
},
5905 {"set3", 0, (test_callback_fn
)set_test3
},
5906 {"dump", 1, (test_callback_fn
)dump_test
},
5907 {"add", 1, (test_callback_fn
)add_test
},
5908 {"replace", 1, (test_callback_fn
)replace_test
},
5909 {"delete", 1, (test_callback_fn
)delete_test
},
5910 {"get", 1, (test_callback_fn
)get_test
},
5911 {"get2", 0, (test_callback_fn
)get_test2
},
5912 {"get3", 0, (test_callback_fn
)get_test3
},
5913 {"get4", 0, (test_callback_fn
)get_test4
},
5914 {"partial mget", 0, (test_callback_fn
)get_test5
},
5915 {"stats_servername", 0, (test_callback_fn
)stats_servername_test
},
5916 {"increment", 0, (test_callback_fn
)increment_test
},
5917 {"increment_with_initial", 1, (test_callback_fn
)increment_with_initial_test
},
5918 {"decrement", 0, (test_callback_fn
)decrement_test
},
5919 {"decrement_with_initial", 1, (test_callback_fn
)decrement_with_initial_test
},
5920 {"increment_by_key", 0, (test_callback_fn
)increment_by_key_test
},
5921 {"increment_with_initial_by_key", 1, (test_callback_fn
)increment_with_initial_by_key_test
},
5922 {"decrement_by_key", 0, (test_callback_fn
)decrement_by_key_test
},
5923 {"decrement_with_initial_by_key", 1, (test_callback_fn
)decrement_with_initial_by_key_test
},
5924 {"quit", 0, (test_callback_fn
)quit_test
},
5925 {"mget", 1, (test_callback_fn
)mget_test
},
5926 {"mget_result", 1, (test_callback_fn
)mget_result_test
},
5927 {"mget_result_alloc", 1, (test_callback_fn
)mget_result_alloc_test
},
5928 {"mget_result_function", 1, (test_callback_fn
)mget_result_function
},
5929 {"mget_execute", 1, (test_callback_fn
)mget_execute
},
5930 {"mget_end", 0, (test_callback_fn
)mget_end
},
5931 {"get_stats", 0, (test_callback_fn
)get_stats
},
5932 {"add_host_test", 0, (test_callback_fn
)add_host_test
},
5933 {"add_host_test_1", 0, (test_callback_fn
)add_host_test1
},
5934 {"get_stats_keys", 0, (test_callback_fn
)get_stats_keys
},
5935 {"version_string_test", 0, (test_callback_fn
)version_string_test
},
5936 {"bad_key", 1, (test_callback_fn
)bad_key_test
},
5937 {"memcached_server_cursor", 1, (test_callback_fn
)memcached_server_cursor_test
},
5938 {"read_through", 1, (test_callback_fn
)read_through
},
5939 {"delete_through", 1, (test_callback_fn
)delete_through
},
5940 {"noreply", 1, (test_callback_fn
)noreply_test
},
5941 {"analyzer", 1, (test_callback_fn
)analyzer_test
},
5942 {"connectionpool", 1, (test_callback_fn
)connection_pool_test
},
5943 {"memcached_pool_test", 1, (test_callback_fn
)memcached_pool_test
},
5944 {"ping", 1, (test_callback_fn
)ping_test
},
5945 {"test_get_last_disconnect", 1, (test_callback_fn
)test_get_last_disconnect
},
5946 {"verbosity", 1, (test_callback_fn
)test_verbosity
},
5947 {"test_server_failure", 1, (test_callback_fn
)test_server_failure
},
5948 {"cull_servers", 1, (test_callback_fn
)test_cull_servers
},
5949 {"memcached_stat_execute", 1, (test_callback_fn
)memcached_stat_execute_test
},
5953 test_st behavior_tests
[] ={
5954 {"behavior_test", 0, (test_callback_fn
)behavior_test
},
5955 {"MEMCACHED_BEHAVIOR_CORK", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_CORK_test
},
5956 {"MEMCACHED_BEHAVIOR_TCP_KEEPALIVE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPALIVE_test
},
5957 {"MEMCACHED_BEHAVIOR_TCP_KEEPIDLE", 0, (test_callback_fn
)MEMCACHED_BEHAVIOR_TCP_KEEPIDLE_test
},
5961 test_st basic_tests
[] ={
5962 {"init", 1, (test_callback_fn
)basic_init_test
},
5963 {"clone", 1, (test_callback_fn
)basic_clone_test
},
5964 {"reset", 1, (test_callback_fn
)basic_reset_stack_test
},
5965 {"reset heap", 1, (test_callback_fn
)basic_reset_heap_test
},
5966 {"reset stack clone", 1, (test_callback_fn
)basic_reset_stack_clone_test
},
5967 {"reset heap clone", 1, (test_callback_fn
)basic_reset_heap_clone_test
},
5971 test_st regression_binary_vs_block
[] ={
5972 {"block add", 1, (test_callback_fn
)block_add_regression
},
5973 {"binary add", 1, (test_callback_fn
)binary_add_regression
},
5977 test_st async_tests
[] ={
5978 {"add", 1, (test_callback_fn
)add_wrapper
},
5982 test_st string_tests
[] ={
5983 {"string static with null", 0, (test_callback_fn
)string_static_null
},
5984 {"string alloc with null", 0, (test_callback_fn
)string_alloc_null
},
5985 {"string alloc with 1K", 0, (test_callback_fn
)string_alloc_with_size
},
5986 {"string alloc with malloc failure", 0, (test_callback_fn
)string_alloc_with_size_toobig
},
5987 {"string append", 0, (test_callback_fn
)string_alloc_append
},
5988 {"string append failure (too big)", 0, (test_callback_fn
)string_alloc_append_toobig
},
5989 {"string_alloc_append_multiple", 0, (test_callback_fn
)string_alloc_append_multiple
},
5990 {0, 0, (test_callback_fn
)0}
5993 test_st result_tests
[] ={
5994 {"result static", 0, (test_callback_fn
)result_static
},
5995 {"result alloc", 0, (test_callback_fn
)result_alloc
},
5996 {0, 0, (test_callback_fn
)0}
5999 test_st version_1_2_3
[] ={
6000 {"append", 0, (test_callback_fn
)append_test
},
6001 {"prepend", 0, (test_callback_fn
)prepend_test
},
6002 {"cas", 0, (test_callback_fn
)cas_test
},
6003 {"cas2", 0, (test_callback_fn
)cas2_test
},
6004 {"append_binary", 0, (test_callback_fn
)append_binary_test
},
6005 {0, 0, (test_callback_fn
)0}
6008 test_st user_tests
[] ={
6009 {"user_supplied_bug1", 0, (test_callback_fn
)user_supplied_bug1
},
6010 {"user_supplied_bug2", 0, (test_callback_fn
)user_supplied_bug2
},
6011 {"user_supplied_bug3", 0, (test_callback_fn
)user_supplied_bug3
},
6012 {"user_supplied_bug4", 0, (test_callback_fn
)user_supplied_bug4
},
6013 {"user_supplied_bug5", 1, (test_callback_fn
)user_supplied_bug5
},
6014 {"user_supplied_bug6", 1, (test_callback_fn
)user_supplied_bug6
},
6015 {"user_supplied_bug7", 1, (test_callback_fn
)user_supplied_bug7
},
6016 {"user_supplied_bug8", 1, (test_callback_fn
)user_supplied_bug8
},
6017 {"user_supplied_bug9", 1, (test_callback_fn
)user_supplied_bug9
},
6018 {"user_supplied_bug10", 1, (test_callback_fn
)user_supplied_bug10
},
6019 {"user_supplied_bug11", 1, (test_callback_fn
)user_supplied_bug11
},
6020 {"user_supplied_bug12", 1, (test_callback_fn
)user_supplied_bug12
},
6021 {"user_supplied_bug13", 1, (test_callback_fn
)user_supplied_bug13
},
6022 {"user_supplied_bug14", 1, (test_callback_fn
)user_supplied_bug14
},
6023 {"user_supplied_bug15", 1, (test_callback_fn
)user_supplied_bug15
},
6024 {"user_supplied_bug16", 1, (test_callback_fn
)user_supplied_bug16
},
6025 #if !defined(__sun) && !defined(__OpenBSD__)
6027 ** It seems to be something weird with the character sets..
6028 ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
6029 ** guess I need to find out how this is supposed to work.. Perhaps I need
6030 ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
6031 ** so just disable the code for now...).
6033 {"user_supplied_bug17", 1, (test_callback_fn
)user_supplied_bug17
},
6035 {"user_supplied_bug18", 1, (test_callback_fn
)user_supplied_bug18
},
6036 {"user_supplied_bug19", 1, (test_callback_fn
)user_supplied_bug19
},
6037 {"user_supplied_bug20", 1, (test_callback_fn
)user_supplied_bug20
},
6038 {"user_supplied_bug21", 1, (test_callback_fn
)user_supplied_bug21
},
6039 {"wrong_failure_counter_test", 1, (test_callback_fn
)wrong_failure_counter_test
},
6040 {"wrong_failure_counter_two_test", 1, (test_callback_fn
)wrong_failure_counter_two_test
},
6041 {0, 0, (test_callback_fn
)0}
6044 test_st replication_tests
[]= {
6045 {"set", 1, (test_callback_fn
)replication_set_test
},
6046 {"get", 0, (test_callback_fn
)replication_get_test
},
6047 {"mget", 0, (test_callback_fn
)replication_mget_test
},
6048 {"delete", 0, (test_callback_fn
)replication_delete_test
},
6049 {"rand_mget", 0, (test_callback_fn
)replication_randomize_mget_test
},
6050 {0, 0, (test_callback_fn
)0}
6054 * The following test suite is used to verify that we don't introduce
6055 * regression bugs. If you want more information about the bug / test,
6056 * you should look in the bug report at
6057 * http://bugs.launchpad.net/libmemcached
6059 test_st regression_tests
[]= {
6060 {"lp:434484", 1, (test_callback_fn
)regression_bug_434484
},
6061 {"lp:434843", 1, (test_callback_fn
)regression_bug_434843
},
6062 {"lp:434843-buffered", 1, (test_callback_fn
)regression_bug_434843_buffered
},
6063 {"lp:421108", 1, (test_callback_fn
)regression_bug_421108
},
6064 {"lp:442914", 1, (test_callback_fn
)regression_bug_442914
},
6065 {"lp:447342", 1, (test_callback_fn
)regression_bug_447342
},
6066 {"lp:463297", 1, (test_callback_fn
)regression_bug_463297
},
6067 {"lp:490486", 1, (test_callback_fn
)regression_bug_490486
},
6068 {"lp:583031", 1, (test_callback_fn
)regression_bug_583031
},
6069 {"lp:?", 1, (test_callback_fn
)regression_bug_
},
6070 {"lp:728286", 1, (test_callback_fn
)regression_bug_728286
},
6071 {"lp:581030", 1, (test_callback_fn
)regression_bug_581030
},
6072 {0, 0, (test_callback_fn
)0}
6075 test_st sasl_auth_tests
[]= {
6076 {"sasl_auth", 1, (test_callback_fn
)sasl_auth_test
},
6077 {0, 0, (test_callback_fn
)0}
6080 test_st ketama_compatibility
[]= {
6081 {"libmemcached", 1, (test_callback_fn
)ketama_compatibility_libmemcached
},
6082 {"spymemcached", 1, (test_callback_fn
)ketama_compatibility_spymemcached
},
6083 {0, 0, (test_callback_fn
)0}
6086 test_st generate_tests
[] ={
6087 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6088 {"generate_data", 1, (test_callback_fn
)generate_data
},
6089 {"get_read", 0, (test_callback_fn
)get_read
},
6090 {"delete_generate", 0, (test_callback_fn
)delete_generate
},
6091 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6092 {"delete_buffer", 0, (test_callback_fn
)delete_buffer_generate
},
6093 {"generate_data", 1, (test_callback_fn
)generate_data
},
6094 {"mget_read", 0, (test_callback_fn
)mget_read
},
6095 {"mget_read_result", 0, (test_callback_fn
)mget_read_result
},
6096 {"mget_read_function", 0, (test_callback_fn
)mget_read_function
},
6097 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6098 {"generate_large_pairs", 1, (test_callback_fn
)generate_large_pairs
},
6099 {"generate_data", 1, (test_callback_fn
)generate_data
},
6100 {"generate_buffer_data", 1, (test_callback_fn
)generate_buffer_data
},
6101 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6102 {0, 0, (test_callback_fn
)0}
6105 test_st consistent_tests
[] ={
6106 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6107 {"generate_data", 1, (test_callback_fn
)generate_data
},
6108 {"get_read", 0, (test_callback_fn
)get_read_count
},
6109 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6110 {0, 0, (test_callback_fn
)0}
6113 test_st consistent_weighted_tests
[] ={
6114 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
6115 {"generate_data", 1, (test_callback_fn
)generate_data_with_stats
},
6116 {"get_read", 0, (test_callback_fn
)get_read_count
},
6117 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
6118 {0, 0, (test_callback_fn
)0}
6121 test_st hsieh_availability
[] ={
6122 {"hsieh_avaibility_test", 0, (test_callback_fn
)hsieh_avaibility_test
},
6123 {0, 0, (test_callback_fn
)0}
6126 test_st murmur_availability
[] ={
6127 {"murmur_avaibility_test", 0, (test_callback_fn
)murmur_avaibility_test
},
6128 {0, 0, (test_callback_fn
)0}
6132 test_st hash_sanity
[] ={
6133 {"hash sanity", 0, (test_callback_fn
)hash_sanity_test
},
6134 {0, 0, (test_callback_fn
)0}
6138 test_st ketama_auto_eject_hosts
[] ={
6139 {"auto_eject_hosts", 1, (test_callback_fn
)auto_eject_hosts
},
6140 {"output_ketama_weighted_keys", 1, (test_callback_fn
)output_ketama_weighted_keys
},
6141 {0, 0, (test_callback_fn
)0}
6144 test_st hash_tests
[] ={
6145 {"one_at_a_time_run", 0, (test_callback_fn
)one_at_a_time_run
},
6146 {"md5", 0, (test_callback_fn
)md5_run
},
6147 {"crc", 0, (test_callback_fn
)crc_run
},
6148 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
6149 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
6150 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
6151 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
6152 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
6153 {"murmur", 0, (test_callback_fn
)murmur_run
},
6154 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
6155 {"memcached_get_hashkit", 0, (test_callback_fn
)memcached_get_hashkit_test
},
6156 {0, 0, (test_callback_fn
)0}
6159 test_st error_conditions
[] ={
6160 {"memcached_get(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_MEMCACHED_ERRNO
},
6161 {"memcached_get(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_MEMCACHED_NOTFOUND
},
6162 {"memcached_get_by_key(MEMCACHED_ERRNO)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_ERRNO
},
6163 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6164 {"memcached_get_by_key(MEMCACHED_NOTFOUND)", 0, (test_callback_fn
)memcached_get_by_key_MEMCACHED_NOTFOUND
},
6165 {"memcached_increment(MEMCACHED_NO_SERVERS)", 0, (test_callback_fn
)memcached_increment_MEMCACHED_NO_SERVERS
},
6166 {0, 0, (test_callback_fn
)0}
6170 test_st parser_tests
[] ={
6171 {"behavior", 0, (test_callback_fn
)behavior_parser_test
},
6172 {"boolean_options", 0, (test_callback_fn
)parser_boolean_options_test
},
6173 {"configure_file", 0, (test_callback_fn
)memcached_create_with_options_with_filename
},
6174 {"distribtions", 0, (test_callback_fn
)parser_distribution_test
},
6175 {"hash", 0, (test_callback_fn
)parser_hash_test
},
6176 {"libmemcached_check_configuration", 0, (test_callback_fn
)libmemcached_check_configuration_test
},
6177 {"libmemcached_check_configuration_with_filename", 0, (test_callback_fn
)libmemcached_check_configuration_with_filename_test
},
6178 {"number_options", 0, (test_callback_fn
)parser_number_options_test
},
6179 {"randomly generated options", 0, (test_callback_fn
)random_statement_build_test
},
6180 {"prefix_key", 0, (test_callback_fn
)parser_key_prefix_test
},
6181 {"server", 0, (test_callback_fn
)server_test
},
6182 {"bad server strings", 0, (test_callback_fn
)servers_bad_test
},
6183 {"server with weights", 0, (test_callback_fn
)server_with_weight_test
},
6184 {0, 0, (test_callback_fn
)0}
6187 test_st virtual_bucket_tests
[] ={
6188 {"basic", 0, (test_callback_fn
)virtual_back_map
},
6189 {0, 0, (test_callback_fn
)0}
6192 collection_st collection
[] ={
6194 {"hash_sanity", 0, 0, hash_sanity
},
6196 {"basic", 0, 0, basic_tests
},
6197 {"hsieh_availability", 0, 0, hsieh_availability
},
6198 {"murmur_availability", 0, 0, murmur_availability
},
6199 {"block", 0, 0, tests
},
6200 {"binary", (test_callback_fn
)pre_binary
, 0, tests
},
6201 {"nonblock", (test_callback_fn
)pre_nonblock
, 0, tests
},
6202 {"nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6203 {"settimer", (test_callback_fn
)pre_settimer
, 0, tests
},
6204 {"md5", (test_callback_fn
)pre_md5
, 0, tests
},
6205 {"crc", (test_callback_fn
)pre_crc
, 0, tests
},
6206 {"hsieh", (test_callback_fn
)pre_hsieh
, 0, tests
},
6207 {"jenkins", (test_callback_fn
)pre_jenkins
, 0, tests
},
6208 {"fnv1_64", (test_callback_fn
)pre_hash_fnv1_64
, 0, tests
},
6209 {"fnv1a_64", (test_callback_fn
)pre_hash_fnv1a_64
, 0, tests
},
6210 {"fnv1_32", (test_callback_fn
)pre_hash_fnv1_32
, 0, tests
},
6211 {"fnv1a_32", (test_callback_fn
)pre_hash_fnv1a_32
, 0, tests
},
6212 {"ketama", (test_callback_fn
)pre_behavior_ketama
, 0, tests
},
6213 {"ketama_auto_eject_hosts", (test_callback_fn
)pre_behavior_ketama
, 0, ketama_auto_eject_hosts
},
6214 {"unix_socket", (test_callback_fn
)pre_unix_socket
, 0, tests
},
6215 {"unix_socket_nodelay", (test_callback_fn
)pre_nodelay
, 0, tests
},
6216 {"poll_timeout", (test_callback_fn
)poll_timeout
, 0, tests
},
6217 {"gets", (test_callback_fn
)enable_cas
, 0, tests
},
6218 {"consistent_crc", (test_callback_fn
)enable_consistent_crc
, 0, tests
},
6219 {"consistent_hsieh", (test_callback_fn
)enable_consistent_hsieh
, 0, tests
},
6220 #ifdef MEMCACHED_ENABLE_DEPRECATED
6221 {"deprecated_memory_allocators", (test_callback_fn
)deprecated_set_memory_alloc
, 0, tests
},
6223 {"memory_allocators", (test_callback_fn
)set_memory_alloc
, 0, tests
},
6224 {"prefix", (test_callback_fn
)set_prefix
, 0, tests
},
6225 {"sasl_auth", (test_callback_fn
)pre_sasl
, 0, sasl_auth_tests
},
6226 {"sasl", (test_callback_fn
)pre_sasl
, 0, tests
},
6227 {"version_1_2_3", (test_callback_fn
)check_for_1_2_3
, 0, version_1_2_3
},
6228 {"string", 0, 0, string_tests
},
6229 {"result", 0, 0, result_tests
},
6230 {"async", (test_callback_fn
)pre_nonblock
, 0, async_tests
},
6231 {"async_binary", (test_callback_fn
)pre_nonblock_binary
, 0, async_tests
},
6232 {"user", 0, 0, user_tests
},
6233 {"generate", 0, 0, generate_tests
},
6234 {"generate_hsieh", (test_callback_fn
)pre_hsieh
, 0, generate_tests
},
6235 {"generate_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, generate_tests
},
6236 {"generate_hsieh_consistent", (test_callback_fn
)enable_consistent_hsieh
, 0, generate_tests
},
6237 {"generate_md5", (test_callback_fn
)pre_md5
, 0, generate_tests
},
6238 {"generate_murmur", (test_callback_fn
)pre_murmur
, 0, generate_tests
},
6239 {"generate_jenkins", (test_callback_fn
)pre_jenkins
, 0, generate_tests
},
6240 {"generate_nonblock", (test_callback_fn
)pre_nonblock
, 0, generate_tests
},
6242 {"generate_corked", (test_callback_fn
)pre_cork
, 0, generate_tests
},
6243 {"generate_corked_and_nonblock", (test_callback_fn
)pre_cork_and_nonblock
, 0, generate_tests
},
6244 {"consistent_not", 0, 0, consistent_tests
},
6245 {"consistent_ketama", (test_callback_fn
)pre_behavior_ketama
, 0, consistent_tests
},
6246 {"consistent_ketama_weighted", (test_callback_fn
)pre_behavior_ketama_weighted
, 0, consistent_weighted_tests
},
6247 {"ketama_compat", 0, 0, ketama_compatibility
},
6248 {"test_hashes", 0, 0, hash_tests
},
6249 {"replication", (test_callback_fn
)pre_replication
, 0, replication_tests
},
6250 {"replication_noblock", (test_callback_fn
)pre_replication_noblock
, 0, replication_tests
},
6251 {"regression", 0, 0, regression_tests
},
6252 {"behaviors", 0, 0, behavior_tests
},
6253 {"regression_binary_vs_block", (test_callback_fn
)key_setup
, (test_callback_fn
)key_teardown
, regression_binary_vs_block
},
6254 {"error_conditions", 0, 0, error_conditions
},
6255 {"parser", 0, 0, parser_tests
},
6256 {"virtual buckets", 0, 0, virtual_bucket_tests
},
6260 #include "tests/libmemcached_world.h"
6262 void get_world(world_st
*world
)
6264 world
->collections
= collection
;
6266 world
->create
= (test_callback_create_fn
)world_create
;
6267 world
->destroy
= (test_callback_fn
)world_destroy
;
6269 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
6270 world
->test
.flush
= (test_callback_fn
)world_flush
;
6271 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
6272 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
6273 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
6275 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
6276 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
6278 world
->runner
= &defualt_libmemcached_runner
;