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 <libmemcachedutil-1.0/util.h>
58 #include <sys/types.h>
64 #include <libtest/server.h>
66 #ifndef __INTEL_COMPILER
67 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
71 @note This should be testing to see if the server really supports the binary protocol.
73 static test_return_t
pre_binary(memcached_st
*memc
)
75 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
76 test_true(memc_clone
);
78 // The memcached_version needs to be done on a clone, because the server
79 // will not toggle protocol on an connection.
80 memcached_version(memc_clone
);
82 test_compare(MEMCACHED_SUCCESS
, memcached_version(memc
));
83 test_compare(true, libmemcached_util_version_check(memc
, 1, 2, 1));
84 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
85 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
87 memcached_free(memc_clone
);
92 typedef std::vector
<uint16_t> Expected
;
94 static void increment_request_id(uint16_t *id
)
97 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
103 static void get_udp_request_ids(memcached_st
*memc
, Expected
&ids
)
105 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
107 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, x
);
109 ids
.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
));
113 static test_return_t
post_udp_op_check(memcached_st
*memc
, Expected
& expected_req_ids
)
116 (void)expected_req_ids
;
118 memcached_server_st
*cur_server
= memcached_server_list(memc
);
119 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
121 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
123 test_true(cur_server
[x
].cursor_active
== 0);
124 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
126 free(expected_req_ids
);
134 ** There is a little bit of a hack here, instead of removing
135 ** the servers, I just set num host to 0 and them add then new udp servers
137 static test_return_t
init_udp(memcached_st
*memc
)
139 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
144 static test_return_t
init_udp_valgrind(memcached_st
*memc
)
146 if (getenv("TESTS_ENVIRONMENT"))
151 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
156 static test_return_t
binary_init_udp(memcached_st
*memc
)
158 if (getenv("TESTS_ENVIRONMENT"))
163 test_skip(TEST_SUCCESS
, pre_binary(memc
));
165 return init_udp(memc
);
168 /* Make sure that I cant add a tcp server to a udp client */
169 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
173 memcached_server_st server
;
174 memcached_server_instance_st instance
=
175 memcached_server_instance_by_position(memc
, 0);
176 memcached_server_clone(&server
, &memc
->hosts
[0]);
177 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
178 test_true(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
183 /* Make sure that I cant add a udp server to a tcp client */
184 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
188 memcached_server_st server
;
189 memcached_server_instance_st instance
=
190 memcached_server_instance_by_position(memc
, 0);
191 memcached_server_clone(&server
, &memc
->hosts
[0]);
192 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
194 memcached_st tcp_client
;
195 memcached_create(&tcp_client
);
196 test_true(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
202 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
204 memcached_quit(memc
);
206 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
));
207 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
208 test_true(memc
->flags
.use_udp
);
209 test_true(memc
->flags
.no_reply
);
211 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, false));
212 test_false(memc
->flags
.use_udp
);
213 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
214 test_false(memc
->flags
.no_reply
);
219 static test_return_t
udp_set_test(memcached_st
*memc
)
221 // Assume we are running under valgrind, and bail
222 if (getenv("TESTS_ENVIRONMENT"))
227 const unsigned int num_iters
= 1025; //request id rolls over at 1024
231 for (size_t x
= 0; x
< num_iters
;x
++)
233 Expected expected_ids
;
234 get_udp_request_ids(memc
, expected_ids
);
235 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
236 test_true(server_key
< memcached_server_count(memc
));
237 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
238 size_t init_offset
= instance
->write_buffer_offset
;
240 memcached_return_t rc
= memcached_set(memc
, test_literal_param("foo"),
241 test_literal_param("when we sanitize"),
242 time_t(0), uint32_t(0));
243 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
244 /** NB, the check below assumes that if new write_ptr is less than
245 * the original write_ptr that we have flushed. For large payloads, this
246 * maybe an invalid assumption, but for the small payload we have it is OK
248 if (rc
== MEMCACHED_SUCCESS
or instance
->write_buffer_offset
< init_offset
)
250 increment_request_id(&expected_ids
[server_key
]);
253 if (rc
== MEMCACHED_SUCCESS
)
255 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
259 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
260 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
263 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
269 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
272 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
276 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
279 char value
[MAX_UDP_DATAGRAM_LENGTH
];
280 Expected expected_ids
;
281 get_udp_request_ids(memc
, expected_ids
);
283 memset(value
, int('f'), sizeof(value
));
285 test_compare_hint(MEMCACHED_WRITE_FAILURE
,
286 memcached_set(memc
, test_literal_param("bar"),
287 test_literal_param(value
),
288 time_t(0), uint32_t(0)),
289 memcached_last_error_message(memc
));
291 return post_udp_op_check(memc
, expected_ids
);
294 static test_return_t
udp_delete_test(memcached_st
*memc
)
298 //request id rolls over at 1024
299 for (size_t x
= 0; x
< 1025; x
++)
301 Expected expected_ids
;
302 get_udp_request_ids(memc
, expected_ids
);
304 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
305 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
306 size_t init_offset
= instance
->write_buffer_offset
;
308 memcached_return_t rc
= memcached_delete(memc
, test_literal_param("foo"), 0);
309 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
311 if (rc
== MEMCACHED_SUCCESS
or instance
->write_buffer_offset
< init_offset
)
313 increment_request_id(&expected_ids
[server_key
]);
316 if (rc
== MEMCACHED_SUCCESS
)
318 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
322 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
323 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
325 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
331 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
333 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
334 return udp_delete_test(memc
);
337 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
339 Expected expected_ids
;
340 get_udp_request_ids(memc
, expected_ids
);
342 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
344 increment_request_id(&expected_ids
[x
]);
347 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
349 return post_udp_op_check(memc
, expected_ids
);
352 static test_return_t
udp_quit_test(memcached_st
*memc
)
354 Expected expected_ids
;
355 memcached_quit(memc
);
357 return post_udp_op_check(memc
, expected_ids
);
360 static test_return_t
udp_flush_test(memcached_st
*memc
)
362 Expected expected_ids
;
363 get_udp_request_ids(memc
, expected_ids
);
365 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
367 increment_request_id(&expected_ids
[x
]);
369 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
371 return post_udp_op_check(memc
, expected_ids
);
374 static test_return_t
udp_incr_test(memcached_st
*memc
)
376 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, test_literal_param("incr"),
377 test_literal_param("1"),
378 (time_t)0, (uint32_t)0));
380 Expected expected_ids
;
381 get_udp_request_ids(memc
, expected_ids
);
383 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
384 increment_request_id(&expected_ids
[server_key
]);
387 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
389 return post_udp_op_check(memc
, expected_ids
);
392 static test_return_t
udp_decr_test(memcached_st
*memc
)
394 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
,
395 test_literal_param("decr"),
396 test_literal_param("1"),
397 (time_t)0, (uint32_t)0));
399 Expected expected_ids
;
400 get_udp_request_ids(memc
, expected_ids
);
402 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("decr"));
403 increment_request_id(&expected_ids
[server_key
]);
406 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
, test_literal_param("decr"), 1, &newvalue
));
408 return post_udp_op_check(memc
, expected_ids
);
412 static test_return_t
udp_stat_test(memcached_st
*memc
)
414 memcached_return_t rc
;
416 Expected expected_ids
;
417 get_udp_request_ids(memc
, expected_ids
);
418 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
419 memcached_stat_free(memc
, rv
);
420 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
422 return post_udp_op_check(memc
, expected_ids
);
425 static test_return_t
udp_version_test(memcached_st
*memc
)
427 Expected expected_ids
;
428 get_udp_request_ids(memc
, expected_ids
);
430 test_compare(MEMCACHED_NOT_SUPPORTED
, memcached_version(memc
));
432 return post_udp_op_check(memc
, expected_ids
);
435 static test_return_t
udp_get_test(memcached_st
*memc
)
437 memcached_return_t rc
;
439 Expected expected_ids
;
440 get_udp_request_ids(memc
, expected_ids
);
441 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
442 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
444 return post_udp_op_check(memc
, expected_ids
);
447 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
449 test_st mixed_io_ops
[] ={
451 (test_callback_fn
*)udp_set_test
},
452 {"udp_set_too_big_test", 0,
453 (test_callback_fn
*)udp_set_too_big_test
},
454 {"udp_delete_test", 0,
455 (test_callback_fn
*)udp_delete_test
},
456 {"udp_verbosity_test", 0,
457 (test_callback_fn
*)udp_verbosity_test
},
459 (test_callback_fn
*)udp_quit_test
},
461 {"udp_flush_test", 0,
462 (test_callback_fn
*)udp_flush_test
},
465 (test_callback_fn
*)udp_incr_test
},
467 (test_callback_fn
*)udp_decr_test
},
468 {"udp_version_test", 0,
469 (test_callback_fn
*)udp_version_test
}
472 for (size_t x
= 0; x
< 500; x
++)
474 test_st current_op
= mixed_io_ops
[(random() % 8)];
475 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
480 test_st udp_setup_server_tests
[] ={
481 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
482 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
483 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
487 test_st upd_io_tests
[] ={
488 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
489 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
490 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
491 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
492 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
493 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
494 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
495 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
496 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
497 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
498 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
499 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
500 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
501 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
505 collection_st collection
[] ={
506 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
507 {"udp_io", (test_callback_fn
*)init_udp_valgrind
, 0, upd_io_tests
},
508 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
512 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
513 #include "tests/libmemcached_world.h"
515 void get_world(Framework
*world
)
517 world
->collections
= collection
;
519 world
->_create
= (test_callback_create_fn
*)world_create
;
520 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
522 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
523 world
->item
._flush
= (test_callback_fn
*)world_flush
;
524 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
525 world
->item
.set_post((test_callback_fn
*)world_post_run
);
526 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
528 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
529 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
531 world
->set_runner(&defualt_libmemcached_runner
);