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