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
set_udp_behavior_test(memcached_st
*memc
)
205 memcached_quit(memc
);
207 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
));
208 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
209 test_compare(true, memc
->flags
.use_udp
);
210 test_compare(false, memc
->flags
.reply
);
212 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, false));
213 test_compare(false, memc
->flags
.use_udp
);
214 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
215 test_compare(true, memc
->flags
.reply
);
220 static test_return_t
udp_set_test(memcached_st
*memc
)
222 // Assume we are running under valgrind, and bail
223 if (getenv("TESTS_ENVIRONMENT"))
228 const unsigned int num_iters
= 1025; //request id rolls over at 1024
232 for (size_t x
= 0; x
< num_iters
;x
++)
234 Expected expected_ids
;
235 get_udp_request_ids(memc
, expected_ids
);
236 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
237 test_true(server_key
< memcached_server_count(memc
));
238 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
239 size_t init_offset
= instance
->write_buffer_offset
;
241 test_compare_hint(MEMCACHED_SUCCESS
,
243 test_literal_param("foo"),
244 test_literal_param("when we sanitize"),
245 time_t(0), uint32_t(0)),
246 memcached_last_error_message(memc
));
249 NB, the check below assumes that if new write_ptr is less than
250 the original write_ptr that we have flushed. For large payloads, this
251 maybe an invalid assumption, but for the small payload we have it is OK
253 if (instance
->write_buffer_offset
< init_offset
)
255 increment_request_id(&expected_ids
[server_key
]);
258 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
264 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
267 test_compare(MEMCACHED_INVALID_ARGUMENTS
,
268 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
272 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
275 char value
[MAX_UDP_DATAGRAM_LENGTH
];
276 Expected expected_ids
;
277 get_udp_request_ids(memc
, expected_ids
);
279 memset(value
, int('f'), sizeof(value
));
281 test_compare_hint(MEMCACHED_WRITE_FAILURE
,
282 memcached_set(memc
, test_literal_param("bar"),
283 test_literal_param(value
),
284 time_t(0), uint32_t(0)),
285 memcached_last_error_message(memc
));
287 return post_udp_op_check(memc
, expected_ids
);
290 static test_return_t
udp_delete_test(memcached_st
*memc
)
294 //request id rolls over at 1024
295 for (size_t x
= 0; x
< 1025; x
++)
297 Expected expected_ids
;
298 get_udp_request_ids(memc
, expected_ids
);
300 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
301 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
302 size_t init_offset
= instance
->write_buffer_offset
;
304 test_compare(MEMCACHED_SUCCESS
,
305 memcached_delete(memc
, test_literal_param("foo"), 0));
307 if (instance
->write_buffer_offset
< init_offset
)
309 increment_request_id(&expected_ids
[server_key
]);
312 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
318 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
320 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
321 return udp_delete_test(memc
);
324 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
326 Expected expected_ids
;
327 get_udp_request_ids(memc
, expected_ids
);
329 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
331 increment_request_id(&expected_ids
[x
]);
334 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
336 return post_udp_op_check(memc
, expected_ids
);
339 static test_return_t
udp_quit_test(memcached_st
*memc
)
341 Expected expected_ids
;
342 memcached_quit(memc
);
344 return post_udp_op_check(memc
, expected_ids
);
347 static test_return_t
udp_flush_test(memcached_st
*memc
)
349 Expected expected_ids
;
350 get_udp_request_ids(memc
, expected_ids
);
352 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
354 increment_request_id(&expected_ids
[x
]);
356 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
358 return post_udp_op_check(memc
, expected_ids
);
361 static test_return_t
udp_incr_test(memcached_st
*memc
)
363 test_compare(MEMCACHED_SUCCESS
,
364 memcached_set(memc
, test_literal_param("incr"),
365 test_literal_param("1"),
366 (time_t)0, (uint32_t)0));
368 Expected expected_ids
;
369 get_udp_request_ids(memc
, expected_ids
);
371 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
372 increment_request_id(&expected_ids
[server_key
]);
375 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
377 return post_udp_op_check(memc
, expected_ids
);
380 static test_return_t
udp_decr_test(memcached_st
*memc
)
382 test_compare(MEMCACHED_SUCCESS
,
384 test_literal_param(__func__
),
385 test_literal_param("1"),
386 (time_t)0, (uint32_t)0));
388 Expected expected_ids
;
389 get_udp_request_ids(memc
, expected_ids
);
391 unsigned int server_key
= memcached_generate_hash(memc
,
392 test_literal_param(__func__
));
393 increment_request_id(&expected_ids
[server_key
]);
396 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
,
397 test_literal_param(__func__
),
400 return post_udp_op_check(memc
, expected_ids
);
404 static test_return_t
udp_stat_test(memcached_st
*memc
)
406 memcached_return_t rc
;
408 Expected expected_ids
;
409 get_udp_request_ids(memc
, expected_ids
);
410 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
411 memcached_stat_free(memc
, rv
);
412 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
414 return post_udp_op_check(memc
, expected_ids
);
417 static test_return_t
udp_version_test(memcached_st
*memc
)
419 Expected expected_ids
;
420 get_udp_request_ids(memc
, expected_ids
);
422 test_compare(MEMCACHED_NOT_SUPPORTED
,
423 memcached_version(memc
));
425 return post_udp_op_check(memc
, expected_ids
);
428 static test_return_t
udp_get_test(memcached_st
*memc
)
430 memcached_return_t rc
;
432 Expected expected_ids
;
433 get_udp_request_ids(memc
, expected_ids
);
434 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
435 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
437 return post_udp_op_check(memc
, expected_ids
);
440 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
442 test_st mixed_io_ops
[] ={
444 (test_callback_fn
*)udp_set_test
},
445 {"udp_set_too_big_test", 0,
446 (test_callback_fn
*)udp_set_too_big_test
},
447 {"udp_delete_test", 0,
448 (test_callback_fn
*)udp_delete_test
},
449 {"udp_verbosity_test", 0,
450 (test_callback_fn
*)udp_verbosity_test
},
452 (test_callback_fn
*)udp_quit_test
},
454 {"udp_flush_test", 0,
455 (test_callback_fn
*)udp_flush_test
},
458 (test_callback_fn
*)udp_incr_test
},
460 (test_callback_fn
*)udp_decr_test
},
461 {"udp_version_test", 0,
462 (test_callback_fn
*)udp_version_test
}
465 for (size_t x
= 0; x
< 500; x
++)
467 test_st current_op
= mixed_io_ops
[(random() % 8)];
468 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
473 test_st udp_setup_server_tests
[] ={
474 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
475 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
476 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
480 test_st upd_io_tests
[] ={
481 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
482 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
483 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
484 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
485 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
486 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
487 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
488 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
489 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
490 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
491 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
492 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
493 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
494 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
498 collection_st collection
[] ={
499 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
500 {"udp_io", (test_callback_fn
*)init_udp_valgrind
, 0, upd_io_tests
},
501 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
505 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
506 #include "tests/libmemcached_world.h"
508 void get_world(Framework
*world
)
510 world
->collections
= collection
;
512 world
->_create
= (test_callback_create_fn
*)world_create
;
513 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
515 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
516 world
->item
._flush
= (test_callback_fn
*)world_flush
;
517 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
518 world
->item
.set_post((test_callback_fn
*)world_post_run
);
519 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
521 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
522 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
524 world
->set_runner(&defualt_libmemcached_runner
);