1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 Sample test application.
44 #include <libtest/test.hpp>
46 using namespace libtest
;
48 #include <libmemcached-1.0/memcached.h>
49 #include <libmemcached/server_instance.h>
50 #include <libmemcached/io.h>
51 #include <libmemcached/udp.hpp>
52 #include <libmemcachedutil-1.0/util.h>
58 #include <sys/types.h>
64 #include <libtest/server.h>
66 #include "libmemcached/instance.h"
68 #ifndef __INTEL_COMPILER
69 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
73 @note This should be testing to see if the server really supports the binary protocol.
75 static test_return_t
pre_binary(memcached_st
*memc
)
77 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
78 test_true(memc_clone
);
80 // The memcached_version needs to be done on a clone, because the server
81 // will not toggle protocol on an connection.
82 memcached_version(memc_clone
);
84 test_compare(MEMCACHED_SUCCESS
, memcached_version(memc
));
85 test_compare(true, libmemcached_util_version_check(memc
, 1, 2, 1));
86 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
87 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
89 memcached_free(memc_clone
);
94 typedef std::vector
<uint16_t> Expected
;
96 static void increment_request_id(uint16_t *id
)
99 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
105 static void get_udp_request_ids(memcached_st
*memc
, Expected
&ids
)
107 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
109 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, x
);
111 ids
.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
));
115 static test_return_t
post_udp_op_check(memcached_st
*memc
, Expected
& expected_req_ids
)
118 (void)expected_req_ids
;
120 memcached_server_st
*cur_server
= memcached_server_list(memc
);
121 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
123 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
125 test_true(cur_server
[x
].cursor_active
== 0);
126 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
128 free(expected_req_ids
);
136 ** There is a little bit of a hack here, instead of removing
137 ** the servers, I just set num host to 0 and them add then new udp servers
139 static test_return_t
init_udp(memcached_st
*memc
)
141 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
146 static test_return_t
init_udp_valgrind(memcached_st
*memc
)
148 if (getenv("TESTS_ENVIRONMENT"))
153 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
158 static test_return_t
binary_init_udp(memcached_st
*memc
)
160 if (getenv("TESTS_ENVIRONMENT"))
165 test_skip(TEST_SUCCESS
, pre_binary(memc
));
167 return init_udp(memc
);
170 /* Make sure that I cant add a tcp server to a udp client */
171 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
175 memcached_server_st server
;
176 memcached_server_instance_st instance
=
177 memcached_server_instance_by_position(memc
, 0);
178 memcached_server_clone(&server
, &memc
->hosts
[0]);
179 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
180 test_true(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
185 /* Make sure that I cant add a udp server to a tcp client */
186 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
190 memcached_server_st server
;
191 memcached_server_instance_st instance
=
192 memcached_server_instance_by_position(memc
, 0);
193 memcached_server_clone(&server
, &memc
->hosts
[0]);
194 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
196 memcached_st tcp_client
;
197 memcached_create(&tcp_client
);
198 test_true(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
204 static test_return_t
version_TEST(memcached_st
*memc
)
206 test_compare(MEMCACHED_NOT_SUPPORTED
, memcached_version(memc
));
210 static test_return_t
verbosity_TEST(memcached_st
*memc
)
212 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 0));
216 static test_return_t
memcached_get_TEST(memcached_st
*memc
)
218 memcached_return_t rc
;
219 test_null(memcached_get(memc
,
220 test_literal_param(__func__
),
222 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
227 static test_return_t
memcached_mget_execute_by_key_TEST(memcached_st
*memc
)
230 size_t *key_length
= NULL
;
231 test_compare(MEMCACHED_NOT_SUPPORTED
,
232 memcached_mget_execute_by_key(memc
,
233 test_literal_param(__func__
), // Group key
234 keys
, key_length
, // Actual key
238 0)); // Number of callbacks
243 static test_return_t
memcached_stat_TEST(memcached_st
*memc
)
245 memcached_return_t rc
;
246 test_null(memcached_stat(memc
, 0, &rc
));
247 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
252 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
254 memcached_quit(memc
);
256 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
));
257 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
258 test_compare(true, memc
->flags
.use_udp
);
259 test_compare(false, memc
->flags
.reply
);
261 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, false));
262 test_compare(false, memc
->flags
.use_udp
);
263 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
264 test_compare(true, memc
->flags
.reply
);
269 static test_return_t
udp_set_test(memcached_st
*memc
)
271 // Assume we are running under valgrind, and bail
272 if (getenv("TESTS_ENVIRONMENT"))
277 const unsigned int num_iters
= 1025; //request id rolls over at 1024
281 for (size_t x
= 0; x
< num_iters
;x
++)
283 Expected expected_ids
;
284 get_udp_request_ids(memc
, expected_ids
);
285 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
286 test_true(server_key
< memcached_server_count(memc
));
287 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
288 size_t init_offset
= instance
->write_buffer_offset
;
290 test_compare_hint(MEMCACHED_SUCCESS
,
292 test_literal_param("foo"),
293 test_literal_param("when we sanitize"),
294 time_t(0), uint32_t(0)),
295 memcached_last_error_message(memc
));
298 NB, the check below assumes that if new write_ptr is less than
299 the original write_ptr that we have flushed. For large payloads, this
300 maybe an invalid assumption, but for the small payload we have it is OK
302 if (instance
->write_buffer_offset
< init_offset
)
304 increment_request_id(&expected_ids
[server_key
]);
307 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
313 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
316 test_compare(MEMCACHED_INVALID_ARGUMENTS
,
317 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
321 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
324 Expected expected_ids
;
325 get_udp_request_ids(memc
, expected_ids
);
327 std::vector
<char> value
;
328 value
.resize(1024 * 1024 * 10);
330 test_compare_hint(MEMCACHED_WRITE_FAILURE
,
332 test_literal_param(__func__
),
333 &value
[0], value
.size(),
334 time_t(0), uint32_t(0)),
335 memcached_last_error_message(memc
));
336 memcached_quit(memc
);
338 return post_udp_op_check(memc
, expected_ids
);
341 static test_return_t
udp_delete_test(memcached_st
*memc
)
345 //request id rolls over at 1024
346 for (size_t x
= 0; x
< 1025; x
++)
348 Expected expected_ids
;
349 get_udp_request_ids(memc
, expected_ids
);
351 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
352 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
353 size_t init_offset
= instance
->write_buffer_offset
;
355 test_compare(MEMCACHED_SUCCESS
,
356 memcached_delete(memc
, test_literal_param("foo"), 0));
358 if (instance
->write_buffer_offset
< init_offset
)
360 increment_request_id(&expected_ids
[server_key
]);
363 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
369 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
371 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
372 return udp_delete_test(memc
);
375 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
377 Expected expected_ids
;
378 get_udp_request_ids(memc
, expected_ids
);
380 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
382 increment_request_id(&expected_ids
[x
]);
385 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
387 return post_udp_op_check(memc
, expected_ids
);
390 static test_return_t
udp_quit_test(memcached_st
*memc
)
392 Expected expected_ids
;
393 memcached_quit(memc
);
395 return post_udp_op_check(memc
, expected_ids
);
398 static test_return_t
udp_flush_test(memcached_st
*memc
)
400 Expected expected_ids
;
401 get_udp_request_ids(memc
, expected_ids
);
403 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
405 increment_request_id(&expected_ids
[x
]);
407 memcached_error_print(memc
);
408 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
410 return post_udp_op_check(memc
, expected_ids
);
413 static test_return_t
udp_incr_test(memcached_st
*memc
)
415 test_compare(MEMCACHED_SUCCESS
,
416 memcached_set(memc
, test_literal_param("incr"),
417 test_literal_param("1"),
418 (time_t)0, (uint32_t)0));
420 Expected expected_ids
;
421 get_udp_request_ids(memc
, expected_ids
);
423 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
424 increment_request_id(&expected_ids
[server_key
]);
427 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
429 return post_udp_op_check(memc
, expected_ids
);
432 static test_return_t
udp_decr_test(memcached_st
*memc
)
434 test_compare(MEMCACHED_SUCCESS
,
436 test_literal_param(__func__
),
437 test_literal_param("1"),
438 time_t(0), uint32_t(0)));
440 Expected expected_ids
;
441 get_udp_request_ids(memc
, expected_ids
);
443 unsigned int server_key
= memcached_generate_hash(memc
,
444 test_literal_param(__func__
));
445 increment_request_id(&expected_ids
[server_key
]);
448 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
,
449 test_literal_param(__func__
),
452 return post_udp_op_check(memc
, expected_ids
);
456 static test_return_t
udp_stat_test(memcached_st
*memc
)
458 memcached_return_t rc
;
460 Expected expected_ids
;
461 get_udp_request_ids(memc
, expected_ids
);
462 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
463 memcached_stat_free(memc
, rv
);
464 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
466 return post_udp_op_check(memc
, expected_ids
);
469 static test_return_t
udp_version_test(memcached_st
*memc
)
471 Expected expected_ids
;
472 get_udp_request_ids(memc
, expected_ids
);
474 test_compare(MEMCACHED_NOT_SUPPORTED
,
475 memcached_version(memc
));
477 return post_udp_op_check(memc
, expected_ids
);
480 static test_return_t
udp_get_test(memcached_st
*memc
)
482 memcached_return_t rc
;
484 Expected expected_ids
;
485 get_udp_request_ids(memc
, expected_ids
);
486 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
487 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
489 return post_udp_op_check(memc
, expected_ids
);
492 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
494 test_st mixed_io_ops
[] ={
496 (test_callback_fn
*)udp_set_test
},
497 {"udp_set_too_big_test", 0,
498 (test_callback_fn
*)udp_set_too_big_test
},
499 {"udp_delete_test", 0,
500 (test_callback_fn
*)udp_delete_test
},
501 {"udp_verbosity_test", 0,
502 (test_callback_fn
*)udp_verbosity_test
},
504 (test_callback_fn
*)udp_quit_test
},
506 {"udp_flush_test", 0,
507 (test_callback_fn
*)udp_flush_test
},
510 (test_callback_fn
*)udp_incr_test
},
512 (test_callback_fn
*)udp_decr_test
},
513 {"udp_version_test", 0,
514 (test_callback_fn
*)udp_version_test
}
517 for (size_t x
= 0; x
< 500; x
++)
519 test_st current_op
= mixed_io_ops
[(random() % 8)];
520 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
525 test_st compatibility_TESTS
[] ={
526 {"version", 0, (test_callback_fn
*)version_TEST
},
527 {"version", 0, (test_callback_fn
*)verbosity_TEST
},
528 {"memcached_get()", 0, (test_callback_fn
*)memcached_get_TEST
},
529 {"memcached_mget_execute_by_key()", 0, (test_callback_fn
*)memcached_mget_execute_by_key_TEST
},
530 {"memcached_stat()", 0, (test_callback_fn
*)memcached_stat_TEST
},
534 test_st udp_setup_server_tests
[] ={
535 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
536 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
537 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
541 test_st upd_io_tests
[] ={
542 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
543 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
544 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
545 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
546 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
547 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
548 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
549 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
550 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
551 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
552 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
553 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
554 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
555 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
559 collection_st collection
[] ={
560 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
561 {"compatibility", (test_callback_fn
*)init_udp
, 0, compatibility_TESTS
},
562 {"udp_io", (test_callback_fn
*)init_udp_valgrind
, 0, upd_io_tests
},
563 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
567 #include "tests/libmemcached_world.h"
569 void get_world(libtest::Framework
* world
)
571 world
->collections(collection
);
573 world
->create((test_callback_create_fn
*)world_create
);
574 world
->destroy((test_callback_destroy_fn
*)world_destroy
);
576 world
->set_runner(new LibmemcachedRunner
);