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
176 for (size_t x
= 0; x
< num_iters
;x
++)
178 Expected expected_ids
;
179 get_udp_request_ids(memc
, expected_ids
);
180 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
181 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
182 size_t init_offset
= instance
->write_buffer_offset
;
184 memcached_return_t rc
= memcached_set(memc
, test_literal_param("foo"),
185 test_literal_param("when we sanitize"),
186 (time_t)0, (uint32_t)0);
187 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
188 /** NB, the check below assumes that if new write_ptr is less than
189 * the original write_ptr that we have flushed. For large payloads, this
190 * maybe an invalid assumption, but for the small payload we have it is OK
192 if (rc
== MEMCACHED_SUCCESS
or instance
->write_buffer_offset
< init_offset
)
194 increment_request_id(&expected_ids
[server_key
]);
197 if (rc
== MEMCACHED_SUCCESS
)
199 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
203 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
204 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
207 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
213 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
215 test_compare(MEMCACHED_INVALID_ARGUMENTS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, true));
219 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
221 char value
[MAX_UDP_DATAGRAM_LENGTH
];
222 Expected expected_ids
;
223 get_udp_request_ids(memc
, expected_ids
);
225 memset(value
, int('f'), sizeof(value
));
227 test_compare_hint(MEMCACHED_WRITE_FAILURE
, memcached_set(memc
, test_literal_param("bar"), value
, sizeof(value
), time_t(0), uint32_t(0)),
228 memcached_last_error_message(memc
));
230 return post_udp_op_check(memc
, expected_ids
);
233 static test_return_t
udp_delete_test(memcached_st
*memc
)
235 //request id rolls over at 1024
236 for (size_t x
= 0; x
< 1025; x
++)
238 Expected expected_ids
;
239 get_udp_request_ids(memc
, expected_ids
);
241 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("foo"));
242 memcached_server_instance_st instance
= memcached_server_instance_by_position(memc
, server_key
);
243 size_t init_offset
= instance
->write_buffer_offset
;
245 memcached_return_t rc
= memcached_delete(memc
, test_literal_param("foo"), 0);
246 test_true(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
);
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
);
262 test_compare(TEST_SUCCESS
, post_udp_op_check(memc
, expected_ids
));
268 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
270 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
271 return udp_delete_test(memc
);
274 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
276 Expected expected_ids
;
277 get_udp_request_ids(memc
, expected_ids
);
279 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
281 increment_request_id(&expected_ids
[x
]);
284 test_compare(MEMCACHED_SUCCESS
, memcached_verbosity(memc
, 3));
286 return post_udp_op_check(memc
, expected_ids
);
289 static test_return_t
udp_quit_test(memcached_st
*memc
)
291 Expected expected_ids
;
292 memcached_quit(memc
);
294 return post_udp_op_check(memc
, expected_ids
);
297 static test_return_t
udp_flush_test(memcached_st
*memc
)
299 Expected expected_ids
;
300 get_udp_request_ids(memc
, expected_ids
);
302 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
304 increment_request_id(&expected_ids
[x
]);
306 test_compare_hint(MEMCACHED_SUCCESS
, memcached_flush(memc
, 0), memcached_last_error_message(memc
));
308 return post_udp_op_check(memc
, expected_ids
);
311 static test_return_t
udp_incr_test(memcached_st
*memc
)
313 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, test_literal_param("incr"),
314 test_literal_param("1"),
315 (time_t)0, (uint32_t)0));
317 Expected expected_ids
;
318 get_udp_request_ids(memc
, expected_ids
);
320 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("incr"));
321 increment_request_id(&expected_ids
[server_key
]);
324 test_compare(MEMCACHED_SUCCESS
, memcached_increment(memc
, test_literal_param("incr"), 1, &newvalue
));
326 return post_udp_op_check(memc
, expected_ids
);
329 static test_return_t
udp_decr_test(memcached_st
*memc
)
331 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
,
332 test_literal_param("decr"),
333 test_literal_param("1"),
334 (time_t)0, (uint32_t)0));
336 Expected expected_ids
;
337 get_udp_request_ids(memc
, expected_ids
);
339 unsigned int server_key
= memcached_generate_hash(memc
, test_literal_param("decr"));
340 increment_request_id(&expected_ids
[server_key
]);
343 test_compare(MEMCACHED_SUCCESS
, memcached_decrement(memc
, test_literal_param("decr"), 1, &newvalue
));
345 return post_udp_op_check(memc
, expected_ids
);
349 static test_return_t
udp_stat_test(memcached_st
*memc
)
351 memcached_return_t rc
;
353 Expected expected_ids
;
354 get_udp_request_ids(memc
, expected_ids
);
355 memcached_stat_st
*rv
= memcached_stat(memc
, args
, &rc
);
356 memcached_stat_free(memc
, rv
);
357 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
359 return post_udp_op_check(memc
, expected_ids
);
362 static test_return_t
udp_version_test(memcached_st
*memc
)
364 Expected expected_ids
;
365 get_udp_request_ids(memc
, expected_ids
);
367 test_compare(MEMCACHED_NOT_SUPPORTED
, memcached_version(memc
));
369 return post_udp_op_check(memc
, expected_ids
);
372 static test_return_t
udp_get_test(memcached_st
*memc
)
374 memcached_return_t rc
;
376 Expected expected_ids
;
377 get_udp_request_ids(memc
, expected_ids
);
378 test_null(memcached_get(memc
, test_literal_param("foo"), &vlen
, (uint32_t)0, &rc
));
379 test_compare(MEMCACHED_NOT_SUPPORTED
, rc
);
381 return post_udp_op_check(memc
, expected_ids
);
384 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
386 test_st mixed_io_ops
[] ={
388 (test_callback_fn
*)udp_set_test
},
389 {"udp_set_too_big_test", 0,
390 (test_callback_fn
*)udp_set_too_big_test
},
391 {"udp_delete_test", 0,
392 (test_callback_fn
*)udp_delete_test
},
393 {"udp_verbosity_test", 0,
394 (test_callback_fn
*)udp_verbosity_test
},
396 (test_callback_fn
*)udp_quit_test
},
398 {"udp_flush_test", 0,
399 (test_callback_fn
*)udp_flush_test
},
402 (test_callback_fn
*)udp_incr_test
},
404 (test_callback_fn
*)udp_decr_test
},
405 {"udp_version_test", 0,
406 (test_callback_fn
*)udp_version_test
}
409 for (size_t x
= 0; x
< 500; x
++)
411 test_st current_op
= mixed_io_ops
[(random() % 8)];
412 test_compare(TEST_SUCCESS
, current_op
.test_fn(memc
));
417 test_st udp_setup_server_tests
[] ={
418 {"set_udp_behavior_test", 0, (test_callback_fn
*)set_udp_behavior_test
},
419 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
*)add_tcp_server_udp_client_test
},
420 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
*)add_udp_server_tcp_client_test
},
424 test_st upd_io_tests
[] ={
425 {"udp_set_test", 0, (test_callback_fn
*)udp_set_test
},
426 {"udp_buffered_set_test", 0, (test_callback_fn
*)udp_buffered_set_test
},
427 {"udp_set_too_big_test", 0, (test_callback_fn
*)udp_set_too_big_test
},
428 {"udp_delete_test", 0, (test_callback_fn
*)udp_delete_test
},
429 {"udp_buffered_delete_test", 0, (test_callback_fn
*)udp_buffered_delete_test
},
430 {"udp_verbosity_test", 0, (test_callback_fn
*)udp_verbosity_test
},
431 {"udp_quit_test", 0, (test_callback_fn
*)udp_quit_test
},
432 {"udp_flush_test", 0, (test_callback_fn
*)udp_flush_test
},
433 {"udp_incr_test", 0, (test_callback_fn
*)udp_incr_test
},
434 {"udp_decr_test", 0, (test_callback_fn
*)udp_decr_test
},
435 {"udp_stat_test", 0, (test_callback_fn
*)udp_stat_test
},
436 {"udp_version_test", 0, (test_callback_fn
*)udp_version_test
},
437 {"udp_get_test", 0, (test_callback_fn
*)udp_get_test
},
438 {"udp_mixed_io_test", 0, (test_callback_fn
*)udp_mixed_io_test
},
442 collection_st collection
[] ={
443 {"udp_setup", (test_callback_fn
*)init_udp
, 0, udp_setup_server_tests
},
444 {"udp_io", (test_callback_fn
*)init_udp
, 0, upd_io_tests
},
445 {"udp_binary_io", (test_callback_fn
*)binary_init_udp
, 0, upd_io_tests
},
449 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
450 #include "libmemcached_world.h"
452 void get_world(Framework
*world
)
454 world
->collections
= collection
;
456 world
->_create
= (test_callback_create_fn
*)world_create
;
457 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
459 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
460 world
->item
._flush
= (test_callback_fn
*)world_flush
;
461 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
462 world
->item
.set_post((test_callback_fn
*)world_post_run
);
463 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
465 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
466 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
468 world
->set_runner(&defualt_libmemcached_runner
);