Merge working tree with build tree.
[awesomized/libmemcached] / tests / mem_udp.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
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.
35 *
36 */
37
38
39 /*
40 Sample test application.
41 */
42
43 #include <config.h>
44 #include <libtest/test.hpp>
45
46 using namespace libtest;
47
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>
53
54 #include <cstdio>
55 #include <cstdlib>
56 #include <cstring>
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <signal.h>
61 #include <unistd.h>
62 #include <time.h>
63
64 #include <libtest/server.h>
65
66 #ifndef __INTEL_COMPILER
67 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
68 #endif
69
70 /**
71 @note This should be testing to see if the server really supports the binary protocol.
72 */
73 static test_return_t pre_binary(memcached_st *memc)
74 {
75 memcached_st *memc_clone= memcached_clone(NULL, memc);
76 test_true(memc_clone);
77
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);
81
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));
86
87 memcached_free(memc_clone);
88
89 return TEST_SUCCESS;
90 }
91
92 typedef std::vector<uint16_t> Expected;
93
94 static void increment_request_id(uint16_t *id)
95 {
96 (*id)++;
97 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
98 {
99 *id= 0;
100 }
101 }
102
103 static void get_udp_request_ids(memcached_st *memc, Expected &ids)
104 {
105 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
106 {
107 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
108
109 ids.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer));
110 }
111 }
112
113 static test_return_t post_udp_op_check(memcached_st *memc, Expected& expected_req_ids)
114 {
115 (void)memc;
116 (void)expected_req_ids;
117 #if 0
118 memcached_server_st *cur_server = memcached_server_list(memc);
119 uint16_t *cur_req_ids = get_udp_request_ids(memc);
120
121 for (size_t x= 0; x < memcached_server_count(memc); x++)
122 {
123 test_true(cur_server[x].cursor_active == 0);
124 test_true(cur_req_ids[x] == expected_req_ids[x]);
125 }
126 free(expected_req_ids);
127 free(cur_req_ids);
128
129 #endif
130 return TEST_SUCCESS;
131 }
132
133 /*
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
136 **/
137 static test_return_t init_udp(memcached_st *memc)
138 {
139 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
140
141 return TEST_SUCCESS;
142 }
143
144 static test_return_t init_udp_valgrind(memcached_st *memc)
145 {
146 if (getenv("TESTS_ENVIRONMENT"))
147 {
148 return TEST_SKIPPED;
149 }
150
151 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
152
153 return TEST_SUCCESS;
154 }
155
156 static test_return_t binary_init_udp(memcached_st *memc)
157 {
158 if (getenv("TESTS_ENVIRONMENT"))
159 {
160 return TEST_SKIPPED;
161 }
162
163 test_skip(TEST_SUCCESS, pre_binary(memc));
164
165 return init_udp(memc);
166 }
167
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)
170 {
171 (void)memc;
172 #if 0
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);
179 #endif
180 return TEST_SUCCESS;
181 }
182
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)
185 {
186 (void)memc;
187 #if 0
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);
193
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);
197 #endif
198
199 return TEST_SUCCESS;
200 }
201
202 static test_return_t version_TEST(memcached_st *memc)
203 {
204 test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
205 return TEST_SUCCESS;
206 }
207
208 static test_return_t verbosity_TEST(memcached_st *memc)
209 {
210 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 0));
211 return TEST_SUCCESS;
212 }
213
214 static test_return_t memcached_get_TEST(memcached_st *memc)
215 {
216 memcached_return_t rc;
217 test_null(memcached_get(memc,
218 test_literal_param(__func__),
219 0, 0, &rc));
220 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
221
222 return TEST_SUCCESS;
223 }
224
225 static test_return_t memcached_mget_execute_by_key_TEST(memcached_st *memc)
226 {
227 char **keys= NULL;
228 size_t *key_length= NULL;
229 test_compare(MEMCACHED_NOT_SUPPORTED,
230 memcached_mget_execute_by_key(memc,
231 test_literal_param(__func__), // Group key
232 keys, key_length, // Actual key
233 0, // Number of keys
234 0, // callbacks
235 0, // context
236 0)); // Number of callbacks
237
238 return TEST_SUCCESS;
239 }
240
241 static test_return_t memcached_stat_TEST(memcached_st *memc)
242 {
243 memcached_return_t rc;
244 test_null(memcached_stat(memc, 0, &rc));
245 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
246
247 return TEST_SUCCESS;
248 }
249
250 static test_return_t set_udp_behavior_test(memcached_st *memc)
251 {
252 memcached_quit(memc);
253
254 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution));
255 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
256 test_compare(true, memc->flags.use_udp);
257 test_compare(false, memc->flags.reply);
258
259 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, false));
260 test_compare(false, memc->flags.use_udp);
261 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, false));
262 test_compare(true, memc->flags.reply);
263
264 return TEST_SUCCESS;
265 }
266
267 static test_return_t udp_set_test(memcached_st *memc)
268 {
269 // Assume we are running under valgrind, and bail
270 if (getenv("TESTS_ENVIRONMENT"))
271 {
272 return TEST_SUCCESS;
273 }
274
275 const unsigned int num_iters= 1025; //request id rolls over at 1024
276
277 test_true(memc);
278
279 for (size_t x= 0; x < num_iters;x++)
280 {
281 Expected expected_ids;
282 get_udp_request_ids(memc, expected_ids);
283 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
284 test_true(server_key < memcached_server_count(memc));
285 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
286 size_t init_offset= instance->write_buffer_offset;
287
288 test_compare_hint(MEMCACHED_SUCCESS,
289 memcached_set(memc,
290 test_literal_param("foo"),
291 test_literal_param("when we sanitize"),
292 time_t(0), uint32_t(0)),
293 memcached_last_error_message(memc));
294
295 /*
296 NB, the check below assumes that if new write_ptr is less than
297 the original write_ptr that we have flushed. For large payloads, this
298 maybe an invalid assumption, but for the small payload we have it is OK
299 */
300 if (instance->write_buffer_offset < init_offset)
301 {
302 increment_request_id(&expected_ids[server_key]);
303 }
304
305 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
306 }
307
308 return TEST_SUCCESS;
309 }
310
311 static test_return_t udp_buffered_set_test(memcached_st *memc)
312 {
313 test_true(memc);
314 test_compare(MEMCACHED_INVALID_ARGUMENTS,
315 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
316 return TEST_SUCCESS;
317 }
318
319 static test_return_t udp_set_too_big_test(memcached_st *memc)
320 {
321 test_true(memc);
322 Expected expected_ids;
323 get_udp_request_ids(memc, expected_ids);
324
325 std::vector<char> value;
326 value.resize(1024 * 1024 * 10);
327
328 test_compare_hint(MEMCACHED_WRITE_FAILURE,
329 memcached_set(memc,
330 test_literal_param(__func__),
331 &value[0], value.size(),
332 time_t(0), uint32_t(0)),
333 memcached_last_error_message(memc));
334 memcached_quit(memc);
335
336 return post_udp_op_check(memc, expected_ids);
337 }
338
339 static test_return_t udp_delete_test(memcached_st *memc)
340 {
341 test_true(memc);
342
343 //request id rolls over at 1024
344 for (size_t x= 0; x < 1025; x++)
345 {
346 Expected expected_ids;
347 get_udp_request_ids(memc, expected_ids);
348
349 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
350 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
351 size_t init_offset= instance->write_buffer_offset;
352
353 test_compare(MEMCACHED_SUCCESS,
354 memcached_delete(memc, test_literal_param("foo"), 0));
355
356 if (instance->write_buffer_offset < init_offset)
357 {
358 increment_request_id(&expected_ids[server_key]);
359 }
360
361 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
362 }
363
364 return TEST_SUCCESS;
365 }
366
367 static test_return_t udp_buffered_delete_test(memcached_st *memc)
368 {
369 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
370 return udp_delete_test(memc);
371 }
372
373 static test_return_t udp_verbosity_test(memcached_st *memc)
374 {
375 Expected expected_ids;
376 get_udp_request_ids(memc, expected_ids);
377
378 for (size_t x= 0; x < memcached_server_count(memc); x++)
379 {
380 increment_request_id(&expected_ids[x]);
381 }
382
383 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
384
385 return post_udp_op_check(memc, expected_ids);
386 }
387
388 static test_return_t udp_quit_test(memcached_st *memc)
389 {
390 Expected expected_ids;
391 memcached_quit(memc);
392
393 return post_udp_op_check(memc, expected_ids);
394 }
395
396 static test_return_t udp_flush_test(memcached_st *memc)
397 {
398 Expected expected_ids;
399 get_udp_request_ids(memc, expected_ids);
400
401 for (size_t x= 0; x < memcached_server_count(memc); x++)
402 {
403 increment_request_id(&expected_ids[x]);
404 }
405 memcached_error_print(memc);
406 test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
407
408 return post_udp_op_check(memc, expected_ids);
409 }
410
411 static test_return_t udp_incr_test(memcached_st *memc)
412 {
413 test_compare(MEMCACHED_SUCCESS,
414 memcached_set(memc, test_literal_param("incr"),
415 test_literal_param("1"),
416 (time_t)0, (uint32_t)0));
417
418 Expected expected_ids;
419 get_udp_request_ids(memc, expected_ids);
420
421 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
422 increment_request_id(&expected_ids[server_key]);
423
424 uint64_t newvalue;
425 test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
426
427 return post_udp_op_check(memc, expected_ids);
428 }
429
430 static test_return_t udp_decr_test(memcached_st *memc)
431 {
432 test_compare(MEMCACHED_SUCCESS,
433 memcached_set(memc,
434 test_literal_param(__func__),
435 test_literal_param("1"),
436 time_t(0), uint32_t(0)));
437
438 Expected expected_ids;
439 get_udp_request_ids(memc, expected_ids);
440
441 unsigned int server_key= memcached_generate_hash(memc,
442 test_literal_param(__func__));
443 increment_request_id(&expected_ids[server_key]);
444
445 uint64_t newvalue;
446 test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc,
447 test_literal_param(__func__),
448 1, &newvalue));
449
450 return post_udp_op_check(memc, expected_ids);
451 }
452
453
454 static test_return_t udp_stat_test(memcached_st *memc)
455 {
456 memcached_return_t rc;
457 char args[]= "";
458 Expected expected_ids;
459 get_udp_request_ids(memc, expected_ids);
460 memcached_stat_st *rv= memcached_stat(memc, args, &rc);
461 memcached_stat_free(memc, rv);
462 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
463
464 return post_udp_op_check(memc, expected_ids);
465 }
466
467 static test_return_t udp_version_test(memcached_st *memc)
468 {
469 Expected expected_ids;
470 get_udp_request_ids(memc, expected_ids);
471
472 test_compare(MEMCACHED_NOT_SUPPORTED,
473 memcached_version(memc));
474
475 return post_udp_op_check(memc, expected_ids);
476 }
477
478 static test_return_t udp_get_test(memcached_st *memc)
479 {
480 memcached_return_t rc;
481 size_t vlen;
482 Expected expected_ids;
483 get_udp_request_ids(memc, expected_ids);
484 test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
485 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
486
487 return post_udp_op_check(memc, expected_ids);
488 }
489
490 static test_return_t udp_mixed_io_test(memcached_st *memc)
491 {
492 test_st mixed_io_ops [] ={
493 {"udp_set_test", 0,
494 (test_callback_fn*)udp_set_test},
495 {"udp_set_too_big_test", 0,
496 (test_callback_fn*)udp_set_too_big_test},
497 {"udp_delete_test", 0,
498 (test_callback_fn*)udp_delete_test},
499 {"udp_verbosity_test", 0,
500 (test_callback_fn*)udp_verbosity_test},
501 {"udp_quit_test", 0,
502 (test_callback_fn*)udp_quit_test},
503 #if 0
504 {"udp_flush_test", 0,
505 (test_callback_fn*)udp_flush_test},
506 #endif
507 {"udp_incr_test", 0,
508 (test_callback_fn*)udp_incr_test},
509 {"udp_decr_test", 0,
510 (test_callback_fn*)udp_decr_test},
511 {"udp_version_test", 0,
512 (test_callback_fn*)udp_version_test}
513 };
514
515 for (size_t x= 0; x < 500; x++)
516 {
517 test_st current_op= mixed_io_ops[(random() % 8)];
518 test_compare(TEST_SUCCESS, current_op.test_fn(memc));
519 }
520 return TEST_SUCCESS;
521 }
522
523 test_st compatibility_TESTS[] ={
524 {"version", 0, (test_callback_fn*)version_TEST },
525 {"version", 0, (test_callback_fn*)verbosity_TEST },
526 {"memcached_get()", 0, (test_callback_fn*)memcached_get_TEST },
527 {"memcached_mget_execute_by_key()", 0, (test_callback_fn*)memcached_mget_execute_by_key_TEST },
528 {"memcached_stat()", 0, (test_callback_fn*)memcached_stat_TEST },
529 {0, 0, 0}
530 };
531
532 test_st udp_setup_server_tests[] ={
533 {"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
534 {"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
535 {"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
536 {0, 0, 0}
537 };
538
539 test_st upd_io_tests[] ={
540 {"udp_set_test", 0, (test_callback_fn*)udp_set_test},
541 {"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
542 {"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
543 {"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
544 {"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
545 {"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
546 {"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
547 {"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
548 {"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
549 {"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
550 {"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
551 {"udp_version_test", 0, (test_callback_fn*)udp_version_test},
552 {"udp_get_test", 0, (test_callback_fn*)udp_get_test},
553 {"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
554 {0, 0, 0}
555 };
556
557 collection_st collection[] ={
558 {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
559 {"compatibility", (test_callback_fn*)init_udp, 0, compatibility_TESTS},
560 {"udp_io", (test_callback_fn*)init_udp_valgrind, 0, upd_io_tests},
561 {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
562 {0, 0, 0, 0}
563 };
564
565 #include "tests/libmemcached_world.h"
566
567 void get_world(Framework *world)
568 {
569 world->collections= collection;
570
571 world->_create= (test_callback_create_fn*)world_create;
572 world->_destroy= (test_callback_destroy_fn*)world_destroy;
573
574 world->item._startup= (test_callback_fn*)world_test_startup;
575 world->item._flush= (test_callback_fn*)world_flush;
576 world->_on_error= (test_callback_error_fn*)world_on_error;
577
578 world->collection_startup= (test_callback_fn*)world_container_startup;
579 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
580
581 world->set_runner(new LibmemcachedRunner);
582 }