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>
59 #include <sys/types.h>
65 #include <libtest/server.h>
67 #ifndef __INTEL_COMPILER
68 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
72 @note This should be testing to see if the server really supports the binary protocol.
74 static test_return_t
pre_binary(memcached_st
*memc
)
76 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
77 test_true(memc_clone
);
79 // The memcached_version needs to be done on a clone, because the server
80 // will not toggle protocol on an connection.
81 memcached_version(memc_clone
);
83 test_compare(MEMCACHED_SUCCESS
, memcached_version(memc
));
84 test_compare(true, libmemcached_util_version_check(memc
, 1, 2, 1));
85 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
86 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
88 memcached_free(memc_clone
);
93 typedef std::vector
<uint16_t> Expected
;
95 static void increment_request_id(uint16_t *id
)
98 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
104 static void get_udp_request_ids(memcached_st
*memc
, Expected
&ids
)
106 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
108 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, x
);
110 ids
.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
));
114 static test_return_t
post_udp_op_check(memcached_st
*memc
, Expected
& expected_req_ids
)
117 (void)expected_req_ids
;
119 memcached_server_st
*cur_server
= memcached_server_list(memc
);
120 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
122 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
124 test_true(cur_server
[x
].cursor_active
== 0);
125 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
127 free(expected_req_ids
);
135 ** There is a little bit of a hack here, instead of removing
136 ** the servers, I just set num host to 0 and them add then new udp servers
138 static test_return_t
init_udp(memcached_st
*memc
)
140 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
145 static test_return_t
init_udp_valgrind(memcached_st
*memc
)
147 if (getenv("TESTS_ENVIRONMENT"))
152 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
157 static test_return_t
binary_init_udp(memcached_st
*memc
)
159 if (getenv("TESTS_ENVIRONMENT"))
164 test_skip(TEST_SUCCESS
, pre_binary(memc
));
166 return init_udp(memc
);
169 /* Make sure that I cant add a tcp server to a udp client */
170 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
174 memcached_server_st server
;
175 memcached_server_instance_st instance
=
176 memcached_server_instance_by_position(memc
, 0);
177 memcached_server_clone(&server
, &memc
->hosts
[0]);
178 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
179 test_true(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
184 /* Make sure that I cant add a udp server to a tcp client */
185 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
189 memcached_server_st server
;
190 memcached_server_instance_st instance
=
191 memcached_server_instance_by_position(memc
, 0);
192 memcached_server_clone(&server
, &memc
->hosts
[0]);
193 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
195 memcached_st tcp_client
;
196 memcached_create(&tcp_client
);
197 test_true(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
203 static test_return_t
version_TEST(memcached_st
*memc
)
205 test_compare(MEMCACHED_NOT_SUPPORTED
, memcached_version(memc
));
209 static test_return_t
verbosity_TEST(memcached_st
*memc
)
211 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 0));
215 static test_return_t
memcached_get_TEST(memcached_st
*memc
)
217 memcached_return_t rc
;
218 test_null(memcached_get(memc
,
219 test_literal_param(__func__
),
221 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
226 static test_return_t
memcached_mget_execute_by_key_TEST(memcached_st
*memc
)
229 size_t *key_length
= NULL
;
230 test_compare(MEMCACHED_NOT_SUPPORTED
,
231 memcached_mget_execute_by_key(memc
,
232 test_literal_param(__func__
), // Group key
233 keys
, key_length
, // Actual key
237 0)); // Number of callbacks
242 static test_return_t
memcached_stat_TEST(memcached_st
*memc
)
244 memcached_return_t rc
;
245 test_null(memcached_stat(memc
, 0, &rc
));
246 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
251 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
253 memcached_quit(memc
);
255 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
));
256 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
257 test_compare(true, memc
->flags
.use_udp
);
258 test_compare(false, memc
->flags
.reply
);
260 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, false));
261 test_compare(false, memc
->flags
.use_udp
);
262 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
263 test_compare(true, memc
->flags
.reply
);
268 static test_return_t
udp_set_test(memcached_st
*memc
)
270 // Assume we are running under valgrind, and bail
271 if (getenv("TESTS_ENVIRONMENT"))
276 const unsigned int num_iters
= 1025; //request id rolls over at 1024
280 for (size_t x
= 0; x
< num_iters
;x
++)
282 Expected expected_ids
;
283 get_udp_request_ids(memc
, expected_ids
);
284 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
285 test_true(server_key
< memcached_server_count(memc
));
286 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
287 size_t init_offset
= instance
->write_buffer_offset
;
289 test_compare_hint(MEMCACHED_SUCCESS
,
291 test_literal_param("foo"),
292 test_literal_param("when we sanitize"),
293 time_t(0), uint32_t(0)),
294 memcached_last_error_message(memc
));
297 NB, the check below assumes that if new write_ptr is less than
298 the original write_ptr that we have flushed. For large payloads, this
299 maybe an invalid assumption, but for the small payload we have it is OK
301 if (instance
->write_buffer_offset
< init_offset
)
303 increment_request_id(&expected_ids
[server_key
]);
306 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
312 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
315 test_compare(MEMCACHED_INVALID_ARGUMENTS
,
316 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
320 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
323 Expected expected_ids
;
324 get_udp_request_ids(memc
, expected_ids
);
326 std::vector
<char> value
;
327 value
.resize(1024 * 1024 * 10);
329 test_compare_hint(MEMCACHED_WRITE_FAILURE
,
331 test_literal_param(__func__
),
332 &value
[0], value
.size(),
333 time_t(0), uint32_t(0)),
334 memcached_last_error_message(memc
));
335 memcached_quit(memc
);
337 return post_udp_op_check(memc
, expected_ids
);
340 static test_return_t
udp_delete_test(memcached_st
*memc
)
344 //request id rolls over at 1024
345 for (size_t x
= 0; x
< 1025; x
++)
347 Expected expected_ids
;
348 get_udp_request_ids(memc
, expected_ids
);
350 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
351 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
352 size_t init_offset
= instance
->write_buffer_offset
;
354 test_compare(MEMCACHED_SUCCESS
,
355 memcached_delete(memc
, test_literal_param("foo"), 0));
357 if (instance
->write_buffer_offset
< init_offset
)
359 increment_request_id(&expected_ids
[server_key
]);
362 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
368 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
370 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
371 return udp_delete_test(memc
);
374 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
376 Expected expected_ids
;
377 get_udp_request_ids(memc
, expected_ids
);
379 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
381 increment_request_id(&expected_ids
[x
]);
384 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
386 return post_udp_op_check(memc
, expected_ids
);
389 static test_return_t
udp_quit_test(memcached_st
*memc
)
391 Expected expected_ids
;
392 memcached_quit(memc
);
394 return post_udp_op_check(memc
, expected_ids
);
397 static test_return_t
udp_flush_test(memcached_st
*memc
)
399 Expected expected_ids
;
400 get_udp_request_ids(memc
, expected_ids
);
402 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
404 increment_request_id(&expected_ids
[x
]);
406 memcached_error_print(memc
);
407 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
409 return post_udp_op_check(memc
, expected_ids
);
412 static test_return_t
udp_incr_test(memcached_st
*memc
)
414 test_compare(MEMCACHED_SUCCESS
,
415 memcached_set(memc
, test_literal_param("incr"),
416 test_literal_param("1"),
417 (time_t)0, (uint32_t)0));
419 Expected expected_ids
;
420 get_udp_request_ids(memc
, expected_ids
);
422 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
423 increment_request_id(&expected_ids
[server_key
]);
426 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
428 return post_udp_op_check(memc
, expected_ids
);
431 static test_return_t
udp_decr_test(memcached_st
*memc
)
433 test_compare(MEMCACHED_SUCCESS
,
435 test_literal_param(__func__
),
436 test_literal_param("1"),
437 time_t(0), uint32_t(0)));
439 Expected expected_ids
;
440 get_udp_request_ids(memc
, expected_ids
);
442 unsigned int server_key
= memcached_generate_hash(memc
,
443 test_literal_param(__func__
));
444 increment_request_id(&expected_ids
[server_key
]);
447 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
,
448 test_literal_param(__func__
),
451 return post_udp_op_check(memc
, expected_ids
);
455 static test_return_t
udp_stat_test(memcached_st
*memc
)
457 memcached_return_t rc
;
459 Expected expected_ids
;
460 get_udp_request_ids(memc
, expected_ids
);
461 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
462 memcached_stat_free(memc
, rv
);
463 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
465 return post_udp_op_check(memc
, expected_ids
);
468 static test_return_t
udp_version_test(memcached_st
*memc
)
470 Expected expected_ids
;
471 get_udp_request_ids(memc
, expected_ids
);
473 test_compare(MEMCACHED_NOT_SUPPORTED
,
474 memcached_version(memc
));
476 return post_udp_op_check(memc
, expected_ids
);
479 static test_return_t
udp_get_test(memcached_st
*memc
)
481 memcached_return_t rc
;
483 Expected expected_ids
;
484 get_udp_request_ids(memc
, expected_ids
);
485 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
486 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
488 return post_udp_op_check(memc
, expected_ids
);
491 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
493 test_st mixed_io_ops
[] ={
495 (test_callback_fn
*)udp_set_test
},
496 {"udp_set_too_big_test", 0,
497 (test_callback_fn
*)udp_set_too_big_test
},
498 {"udp_delete_test", 0,
499 (test_callback_fn
*)udp_delete_test
},
500 {"udp_verbosity_test", 0,
501 (test_callback_fn
*)udp_verbosity_test
},
503 (test_callback_fn
*)udp_quit_test
},
505 {"udp_flush_test", 0,
506 (test_callback_fn
*)udp_flush_test
},
509 (test_callback_fn
*)udp_incr_test
},
511 (test_callback_fn
*)udp_decr_test
},
512 {"udp_version_test", 0,
513 (test_callback_fn
*)udp_version_test
}
516 for (size_t x
= 0; x
< 500; x
++)
518 test_st current_op
= mixed_io_ops
[(random() % 8)];
519 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
524 test_st compatibility_TESTS
[] ={
525 {"version", 0, (test_callback_fn
*)version_TEST
},
526 {"version", 0, (test_callback_fn
*)verbosity_TEST
},
527 {"memcached_get()", 0, (test_callback_fn
*)memcached_get_TEST
},
528 {"memcached_mget_execute_by_key()", 0, (test_callback_fn
*)memcached_mget_execute_by_key_TEST
},
529 {"memcached_stat()", 0, (test_callback_fn
*)memcached_stat_TEST
},
533 test_st udp_setup_server_tests
[] ={
534 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
535 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
536 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
540 test_st upd_io_tests
[] ={
541 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
542 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
543 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
544 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
545 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
546 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
547 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
548 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
549 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
550 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
551 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
552 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
553 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
554 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
558 collection_st collection
[] ={
559 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
560 {"compatibility", (test_callback_fn
*)init_udp
, 0, compatibility_TESTS
},
561 {"udp_io", (test_callback_fn
*)init_udp_valgrind
, 0, upd_io_tests
},
562 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
566 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
567 #include "tests/libmemcached_world.h"
569 void get_world(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
->item
._startup
= (test_callback_fn
*)world_test_startup
;
577 world
->item
._flush
= (test_callback_fn
*)world_flush
;
578 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
579 world
->item
.set_post((test_callback_fn
*)world_post_run
);
580 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
582 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
583 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
585 world
->set_runner(&defualt_libmemcached_runner
);