Merge in trunk
[m6w6/libmemcached] / tests / mem_udp.cc
1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 /*
10 Sample test application.
11 */
12
13 #include <config.h>
14 #include <libtest/test.hpp>
15
16 using namespace libtest;
17
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>
22
23 #include <cassert>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <time.h>
33
34 #include <libtest/server.h>
35
36 #ifndef __INTEL_COMPILER
37 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
38 #endif
39
40 /**
41 @note This should be testing to see if the server really supports the binary protocol.
42 */
43 static test_return_t pre_binary(memcached_st *memc)
44 {
45 memcached_st *memc_clone= memcached_clone(NULL, memc);
46 test_true(memc_clone);
47
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);
51
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));
56
57 memcached_free(memc_clone);
58
59 return TEST_SUCCESS;
60 }
61
62 typedef std::vector<uint16_t> Expected;
63
64 static void increment_request_id(uint16_t *id)
65 {
66 (*id)++;
67 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
68 {
69 *id= 0;
70 }
71 }
72
73 static void get_udp_request_ids(memcached_st *memc, Expected &ids)
74 {
75 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
76 {
77 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
78
79 ids.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer));
80 }
81 }
82
83 static test_return_t post_udp_op_check(memcached_st *memc, Expected& expected_req_ids)
84 {
85 (void)memc;
86 (void)expected_req_ids;
87 #if 0
88 memcached_server_st *cur_server = memcached_server_list(memc);
89 uint16_t *cur_req_ids = get_udp_request_ids(memc);
90
91 for (size_t x= 0; x < memcached_server_count(memc); x++)
92 {
93 test_true(cur_server[x].cursor_active == 0);
94 test_true(cur_req_ids[x] == expected_req_ids[x]);
95 }
96 free(expected_req_ids);
97 free(cur_req_ids);
98
99 #endif
100 return TEST_SUCCESS;
101 }
102
103 /*
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
106 **/
107 static test_return_t init_udp(memcached_st *memc)
108 {
109 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
110
111 return TEST_SUCCESS;
112 }
113
114 static test_return_t binary_init_udp(memcached_st *memc)
115 {
116 test_skip(TEST_SUCCESS, pre_binary(memc));
117
118 return init_udp(memc);
119 }
120
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)
123 {
124 (void)memc;
125 #if 0
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);
132 #endif
133 return TEST_SUCCESS;
134 }
135
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)
138 {
139 (void)memc;
140 #if 0
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);
146
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);
150 #endif
151
152 return TEST_SUCCESS;
153 }
154
155 static test_return_t set_udp_behavior_test(memcached_st *memc)
156 {
157 memcached_quit(memc);
158
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);
163
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);
168
169 return TEST_SUCCESS;
170 }
171
172 static test_return_t udp_set_test(memcached_st *memc)
173 {
174 unsigned int num_iters= 1025; //request id rolls over at 1024
175
176 test_true(memc);
177
178 for (size_t x= 0; x < num_iters;x++)
179 {
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;
186
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
194 */
195 if (rc == MEMCACHED_SUCCESS or instance->write_buffer_offset < init_offset)
196 {
197 increment_request_id(&expected_ids[server_key]);
198 }
199
200 if (rc == MEMCACHED_SUCCESS)
201 {
202 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
203 }
204 else
205 {
206 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
207 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
208 }
209
210 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
211 }
212
213 return TEST_SUCCESS;
214 }
215
216 static test_return_t udp_buffered_set_test(memcached_st *memc)
217 {
218 test_true(memc);
219 test_compare(MEMCACHED_INVALID_ARGUMENTS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
220 return TEST_SUCCESS;
221 }
222
223 static test_return_t udp_set_too_big_test(memcached_st *memc)
224 {
225 test_true(memc);
226 char value[MAX_UDP_DATAGRAM_LENGTH];
227 Expected expected_ids;
228 get_udp_request_ids(memc, expected_ids);
229
230 memset(value, int('f'), sizeof(value));
231
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));
234
235 return post_udp_op_check(memc, expected_ids);
236 }
237
238 static test_return_t udp_delete_test(memcached_st *memc)
239 {
240 test_true(memc);
241
242 //request id rolls over at 1024
243 for (size_t x= 0; x < 1025; x++)
244 {
245 Expected expected_ids;
246 get_udp_request_ids(memc, expected_ids);
247
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;
251
252 memcached_return_t rc= memcached_delete(memc, test_literal_param("foo"), 0);
253 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
254
255 if (rc == MEMCACHED_SUCCESS or instance->write_buffer_offset < init_offset)
256 {
257 increment_request_id(&expected_ids[server_key]);
258 }
259
260 if (rc == MEMCACHED_SUCCESS)
261 {
262 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
263 }
264 else
265 {
266 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
267 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
268 }
269 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
270 }
271
272 return TEST_SUCCESS;
273 }
274
275 static test_return_t udp_buffered_delete_test(memcached_st *memc)
276 {
277 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
278 return udp_delete_test(memc);
279 }
280
281 static test_return_t udp_verbosity_test(memcached_st *memc)
282 {
283 Expected expected_ids;
284 get_udp_request_ids(memc, expected_ids);
285
286 for (size_t x= 0; x < memcached_server_count(memc); x++)
287 {
288 increment_request_id(&expected_ids[x]);
289 }
290
291 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
292
293 return post_udp_op_check(memc, expected_ids);
294 }
295
296 static test_return_t udp_quit_test(memcached_st *memc)
297 {
298 Expected expected_ids;
299 memcached_quit(memc);
300
301 return post_udp_op_check(memc, expected_ids);
302 }
303
304 static test_return_t udp_flush_test(memcached_st *memc)
305 {
306 Expected expected_ids;
307 get_udp_request_ids(memc, expected_ids);
308
309 for (size_t x= 0; x < memcached_server_count(memc); x++)
310 {
311 increment_request_id(&expected_ids[x]);
312 }
313 test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
314
315 return post_udp_op_check(memc, expected_ids);
316 }
317
318 static test_return_t udp_incr_test(memcached_st *memc)
319 {
320 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, test_literal_param("incr"),
321 test_literal_param("1"),
322 (time_t)0, (uint32_t)0));
323
324 Expected expected_ids;
325 get_udp_request_ids(memc, expected_ids);
326
327 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
328 increment_request_id(&expected_ids[server_key]);
329
330 uint64_t newvalue;
331 test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
332
333 return post_udp_op_check(memc, expected_ids);
334 }
335
336 static test_return_t udp_decr_test(memcached_st *memc)
337 {
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));
342
343 Expected expected_ids;
344 get_udp_request_ids(memc, expected_ids);
345
346 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("decr"));
347 increment_request_id(&expected_ids[server_key]);
348
349 uint64_t newvalue;
350 test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc, test_literal_param("decr"), 1, &newvalue));
351
352 return post_udp_op_check(memc, expected_ids);
353 }
354
355
356 static test_return_t udp_stat_test(memcached_st *memc)
357 {
358 memcached_return_t rc;
359 char args[]= "";
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);
365
366 return post_udp_op_check(memc, expected_ids);
367 }
368
369 static test_return_t udp_version_test(memcached_st *memc)
370 {
371 Expected expected_ids;
372 get_udp_request_ids(memc, expected_ids);
373
374 test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
375
376 return post_udp_op_check(memc, expected_ids);
377 }
378
379 static test_return_t udp_get_test(memcached_st *memc)
380 {
381 memcached_return_t rc;
382 size_t vlen;
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);
387
388 return post_udp_op_check(memc, expected_ids);
389 }
390
391 static test_return_t udp_mixed_io_test(memcached_st *memc)
392 {
393 test_st mixed_io_ops [] ={
394 {"udp_set_test", 0,
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},
402 {"udp_quit_test", 0,
403 (test_callback_fn*)udp_quit_test},
404 #if 0
405 {"udp_flush_test", 0,
406 (test_callback_fn*)udp_flush_test},
407 #endif
408 {"udp_incr_test", 0,
409 (test_callback_fn*)udp_incr_test},
410 {"udp_decr_test", 0,
411 (test_callback_fn*)udp_decr_test},
412 {"udp_version_test", 0,
413 (test_callback_fn*)udp_version_test}
414 };
415
416 for (size_t x= 0; x < 500; x++)
417 {
418 test_st current_op= mixed_io_ops[(random() % 8)];
419 test_compare(TEST_SUCCESS, current_op.test_fn(memc));
420 }
421 return TEST_SUCCESS;
422 }
423
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},
428 {0, 0, 0}
429 };
430
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},
446 {0, 0, 0}
447 };
448
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},
453 {0, 0, 0, 0}
454 };
455
456 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
457 #include "tests/libmemcached_world.h"
458
459 void get_world(Framework *world)
460 {
461 world->collections= collection;
462
463 world->_create= (test_callback_create_fn*)world_create;
464 world->_destroy= (test_callback_destroy_fn*)world_destroy;
465
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;
471
472 world->collection_startup= (test_callback_fn*)world_container_startup;
473 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
474
475 world->set_runner(&defualt_libmemcached_runner);
476 }