1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
14 #include <libtest/test.hpp>
16 using namespace libtest
;
18 #include <libmemcached-1.0/memcached.h>
19 #include <libmemcached/server_instance.h>
20 #include <libmemcached/io.h>
21 #include <libmemcachedutil-1.0/util.h>
28 #include <sys/types.h>
34 #include <libtest/server.h>
36 #ifndef __INTEL_COMPILER
37 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
41 @note This should be testing to see if the server really supports the binary protocol.
43 static test_return_t
pre_binary(memcached_st
*memc
)
45 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
46 test_true(memc_clone
);
48 // The memcached_version needs to be done on a clone, because the server
49 // will not toggle protocol on an connection.
50 memcached_version(memc_clone
);
52 test_compare(MEMCACHED_SUCCESS
, memcached_version(memc
));
53 test_compare(true, libmemcached_util_version_check(memc
, 1, 2, 1));
54 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, true));
55 test_compare(true, memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
));
57 memcached_free(memc_clone
);
62 typedef std::vector
<uint16_t> Expected
;
64 static void increment_request_id(uint16_t *id
)
67 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
73 static void get_udp_request_ids(memcached_st
*memc
, Expected
&ids
)
75 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
77 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, x
);
79 ids
.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
));
83 static test_return_t
post_udp_op_check(memcached_st
*memc
, Expected
& expected_req_ids
)
86 (void)expected_req_ids
;
88 memcached_server_st
*cur_server
= memcached_server_list(memc
);
89 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
91 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
93 test_true(cur_server
[x
].cursor_active
== 0);
94 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
96 free(expected_req_ids
);
104 ** There is a little bit of a hack here, instead of removing
105 ** the servers, I just set num host to 0 and them add then new udp servers
107 static test_return_t
init_udp(memcached_st
*memc
)
109 test_skip(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
114 static test_return_t
binary_init_udp(memcached_st
*memc
)
116 test_skip(TEST_SUCCESS
, pre_binary(memc
));
118 return init_udp(memc
);
121 /* Make sure that I cant add a tcp server to a udp client */
122 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
126 memcached_server_st server
;
127 memcached_server_instance_st instance
=
128 memcached_server_instance_by_position(memc
, 0);
129 memcached_server_clone(&server
, &memc
->hosts
[0]);
130 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
131 test_true(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
136 /* Make sure that I cant add a udp server to a tcp client */
137 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
141 memcached_server_st server
;
142 memcached_server_instance_st instance
=
143 memcached_server_instance_by_position(memc
, 0);
144 memcached_server_clone(&server
, &memc
->hosts
[0]);
145 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
147 memcached_st tcp_client
;
148 memcached_create(&tcp_client
);
149 test_true(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
155 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
157 memcached_quit(memc
);
159 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
));
160 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, true));
161 test_true(memc
->flags
.use_udp
);
162 test_true(memc
->flags
.no_reply
);
164 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, false));
165 test_false(memc
->flags
.use_udp
);
166 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
, false));
167 test_false(memc
->flags
.no_reply
);
172 static test_return_t
udp_set_test(memcached_st
*memc
)
174 unsigned int num_iters
= 1025; //request id rolls over at 1024
178 for (size_t x
= 0; x
< num_iters
;x
++)
180 Expected expected_ids
;
181 get_udp_request_ids(memc
, expected_ids
);
182 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
183 test_true(server_key
< memcached_server_count(memc
));
184 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
185 size_t init_offset
= instance
->write_buffer_offset
;
187 memcached_return_t rc
= memcached_set(memc
, test_literal_param("foo"),
188 test_literal_param("when we sanitize"),
189 time_t(0), uint32_t(0));
190 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
191 /** NB, the check below assumes that if new write_ptr is less than
192 * the original write_ptr that we have flushed. For large payloads, this
193 * maybe an invalid assumption, but for the small payload we have it is OK
195 if (rc
== MEMCACHED_SUCCESS
or instance
->write_buffer_offset
< init_offset
)
197 increment_request_id(&expected_ids
[server_key
]);
200 if (rc
== MEMCACHED_SUCCESS
)
202 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
206 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
207 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
210 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
216 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
219 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
223 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
226 char value
[MAX_UDP_DATAGRAM_LENGTH
];
227 Expected expected_ids
;
228 get_udp_request_ids(memc
, expected_ids
);
230 memset(value
, int('f'), sizeof(value
));
232 test_compare_hint(MEMCACHED_WRITE_FAILURE
, memcached_set(memc
, test_literal_param("bar"), value
, sizeof(value
), time_t(0), uint32_t(0)),
233 memcached_last_error_message(memc
));
235 return post_udp_op_check(memc
, expected_ids
);
238 static test_return_t
udp_delete_test(memcached_st
*memc
)
242 //request id rolls over at 1024
243 for (size_t x
= 0; x
< 1025; x
++)
245 Expected expected_ids
;
246 get_udp_request_ids(memc
, expected_ids
);
248 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
249 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
250 size_t init_offset
= instance
->write_buffer_offset
;
252 memcached_return_t rc
= memcached_delete(memc
, test_literal_param("foo"), 0);
253 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
255 if (rc
== MEMCACHED_SUCCESS
or instance
->write_buffer_offset
< init_offset
)
257 increment_request_id(&expected_ids
[server_key
]);
260 if (rc
== MEMCACHED_SUCCESS
)
262 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
266 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
267 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
269 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
275 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
277 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
278 return udp_delete_test(memc
);
281 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
283 Expected expected_ids
;
284 get_udp_request_ids(memc
, expected_ids
);
286 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
288 increment_request_id(&expected_ids
[x
]);
291 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
293 return post_udp_op_check(memc
, expected_ids
);
296 static test_return_t
udp_quit_test(memcached_st
*memc
)
298 Expected expected_ids
;
299 memcached_quit(memc
);
301 return post_udp_op_check(memc
, expected_ids
);
304 static test_return_t
udp_flush_test(memcached_st
*memc
)
306 Expected expected_ids
;
307 get_udp_request_ids(memc
, expected_ids
);
309 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
311 increment_request_id(&expected_ids
[x
]);
313 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
315 return post_udp_op_check(memc
, expected_ids
);
318 static test_return_t
udp_incr_test(memcached_st
*memc
)
320 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, test_literal_param("incr"),
321 test_literal_param("1"),
322 (time_t)0, (uint32_t)0));
324 Expected expected_ids
;
325 get_udp_request_ids(memc
, expected_ids
);
327 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
328 increment_request_id(&expected_ids
[server_key
]);
331 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
333 return post_udp_op_check(memc
, expected_ids
);
336 static test_return_t
udp_decr_test(memcached_st
*memc
)
338 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
,
339 test_literal_param("decr"),
340 test_literal_param("1"),
341 (time_t)0, (uint32_t)0));
343 Expected expected_ids
;
344 get_udp_request_ids(memc
, expected_ids
);
346 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("decr"));
347 increment_request_id(&expected_ids
[server_key
]);
350 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
, test_literal_param("decr"), 1, &newvalue
));
352 return post_udp_op_check(memc
, expected_ids
);
356 static test_return_t
udp_stat_test(memcached_st
*memc
)
358 memcached_return_t rc
;
360 Expected expected_ids
;
361 get_udp_request_ids(memc
, expected_ids
);
362 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
363 memcached_stat_free(memc
, rv
);
364 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
366 return post_udp_op_check(memc
, expected_ids
);
369 static test_return_t
udp_version_test(memcached_st
*memc
)
371 Expected expected_ids
;
372 get_udp_request_ids(memc
, expected_ids
);
374 test_compare(MEMCACHED_NOT_SUPPORTED
, memcached_version(memc
));
376 return post_udp_op_check(memc
, expected_ids
);
379 static test_return_t
udp_get_test(memcached_st
*memc
)
381 memcached_return_t rc
;
383 Expected expected_ids
;
384 get_udp_request_ids(memc
, expected_ids
);
385 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
386 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
388 return post_udp_op_check(memc
, expected_ids
);
391 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
393 test_st mixed_io_ops
[] ={
395 (test_callback_fn
*)udp_set_test
},
396 {"udp_set_too_big_test", 0,
397 (test_callback_fn
*)udp_set_too_big_test
},
398 {"udp_delete_test", 0,
399 (test_callback_fn
*)udp_delete_test
},
400 {"udp_verbosity_test", 0,
401 (test_callback_fn
*)udp_verbosity_test
},
403 (test_callback_fn
*)udp_quit_test
},
405 {"udp_flush_test", 0,
406 (test_callback_fn
*)udp_flush_test
},
409 (test_callback_fn
*)udp_incr_test
},
411 (test_callback_fn
*)udp_decr_test
},
412 {"udp_version_test", 0,
413 (test_callback_fn
*)udp_version_test
}
416 for (size_t x
= 0; x
< 500; x
++)
418 test_st current_op
= mixed_io_ops
[(random() % 8)];
419 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
424 test_st udp_setup_server_tests
[] ={
425 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
426 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
427 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
431 test_st upd_io_tests
[] ={
432 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
433 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
434 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
435 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
436 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
437 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
438 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
439 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
440 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
441 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
442 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
443 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
444 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
445 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
449 collection_st collection
[] ={
450 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
451 {"udp_io", (test_callback_fn
*)init_udp
, 0, upd_io_tests
},
452 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
456 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
457 #include "tests/libmemcached_world.h"
459 void get_world(Framework
*world
)
461 world
->collections
= collection
;
463 world
->_create
= (test_callback_create_fn
*)world_create
;
464 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
466 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
467 world
->item
._flush
= (test_callback_fn
*)world_flush
;
468 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
469 world
->item
.set_post((test_callback_fn
*)world_post_run
);
470 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
472 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
473 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
475 world
->set_runner(&defualt_libmemcached_runner
);