590ad7b9d816da5bc5f9c9aa70611e41ce1867af
[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 set_udp_behavior_test(memcached_st *memc)
204 {
205 memcached_quit(memc);
206
207 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution));
208 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
209 test_compare(true, memc->flags.use_udp);
210 test_compare(false, memc->flags.reply);
211
212 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, false));
213 test_compare(false, memc->flags.use_udp);
214 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, false));
215 test_compare(true, memc->flags.reply);
216
217 return TEST_SUCCESS;
218 }
219
220 static test_return_t udp_set_test(memcached_st *memc)
221 {
222 // Assume we are running under valgrind, and bail
223 if (getenv("TESTS_ENVIRONMENT"))
224 {
225 return TEST_SUCCESS;
226 }
227
228 const unsigned int num_iters= 1025; //request id rolls over at 1024
229
230 test_true(memc);
231
232 for (size_t x= 0; x < num_iters;x++)
233 {
234 Expected expected_ids;
235 get_udp_request_ids(memc, expected_ids);
236 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
237 test_true(server_key < memcached_server_count(memc));
238 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
239 size_t init_offset= instance->write_buffer_offset;
240
241 test_compare_hint(MEMCACHED_SUCCESS,
242 memcached_set(memc,
243 test_literal_param("foo"),
244 test_literal_param("when we sanitize"),
245 time_t(0), uint32_t(0)),
246 memcached_last_error_message(memc));
247
248 /*
249 NB, the check below assumes that if new write_ptr is less than
250 the original write_ptr that we have flushed. For large payloads, this
251 maybe an invalid assumption, but for the small payload we have it is OK
252 */
253 if (instance->write_buffer_offset < init_offset)
254 {
255 increment_request_id(&expected_ids[server_key]);
256 }
257
258 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
259 }
260
261 return TEST_SUCCESS;
262 }
263
264 static test_return_t udp_buffered_set_test(memcached_st *memc)
265 {
266 test_true(memc);
267 test_compare(MEMCACHED_INVALID_ARGUMENTS,
268 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
269 return TEST_SUCCESS;
270 }
271
272 static test_return_t udp_set_too_big_test(memcached_st *memc)
273 {
274 test_true(memc);
275 char value[MAX_UDP_DATAGRAM_LENGTH];
276 Expected expected_ids;
277 get_udp_request_ids(memc, expected_ids);
278
279 memset(value, int('f'), sizeof(value));
280
281 test_compare_hint(MEMCACHED_WRITE_FAILURE,
282 memcached_set(memc, test_literal_param("bar"),
283 test_literal_param(value),
284 time_t(0), uint32_t(0)),
285 memcached_last_error_message(memc));
286
287 return post_udp_op_check(memc, expected_ids);
288 }
289
290 static test_return_t udp_delete_test(memcached_st *memc)
291 {
292 test_true(memc);
293
294 //request id rolls over at 1024
295 for (size_t x= 0; x < 1025; x++)
296 {
297 Expected expected_ids;
298 get_udp_request_ids(memc, expected_ids);
299
300 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
301 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
302 size_t init_offset= instance->write_buffer_offset;
303
304 test_compare(MEMCACHED_SUCCESS,
305 memcached_delete(memc, test_literal_param("foo"), 0));
306
307 if (instance->write_buffer_offset < init_offset)
308 {
309 increment_request_id(&expected_ids[server_key]);
310 }
311
312 test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
313 }
314
315 return TEST_SUCCESS;
316 }
317
318 static test_return_t udp_buffered_delete_test(memcached_st *memc)
319 {
320 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
321 return udp_delete_test(memc);
322 }
323
324 static test_return_t udp_verbosity_test(memcached_st *memc)
325 {
326 Expected expected_ids;
327 get_udp_request_ids(memc, expected_ids);
328
329 for (size_t x= 0; x < memcached_server_count(memc); x++)
330 {
331 increment_request_id(&expected_ids[x]);
332 }
333
334 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
335
336 return post_udp_op_check(memc, expected_ids);
337 }
338
339 static test_return_t udp_quit_test(memcached_st *memc)
340 {
341 Expected expected_ids;
342 memcached_quit(memc);
343
344 return post_udp_op_check(memc, expected_ids);
345 }
346
347 static test_return_t udp_flush_test(memcached_st *memc)
348 {
349 Expected expected_ids;
350 get_udp_request_ids(memc, expected_ids);
351
352 for (size_t x= 0; x < memcached_server_count(memc); x++)
353 {
354 increment_request_id(&expected_ids[x]);
355 }
356 test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
357
358 return post_udp_op_check(memc, expected_ids);
359 }
360
361 static test_return_t udp_incr_test(memcached_st *memc)
362 {
363 test_compare(MEMCACHED_SUCCESS,
364 memcached_set(memc, test_literal_param("incr"),
365 test_literal_param("1"),
366 (time_t)0, (uint32_t)0));
367
368 Expected expected_ids;
369 get_udp_request_ids(memc, expected_ids);
370
371 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
372 increment_request_id(&expected_ids[server_key]);
373
374 uint64_t newvalue;
375 test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
376
377 return post_udp_op_check(memc, expected_ids);
378 }
379
380 static test_return_t udp_decr_test(memcached_st *memc)
381 {
382 test_compare(MEMCACHED_SUCCESS,
383 memcached_set(memc,
384 test_literal_param(__func__),
385 test_literal_param("1"),
386 (time_t)0, (uint32_t)0));
387
388 Expected expected_ids;
389 get_udp_request_ids(memc, expected_ids);
390
391 unsigned int server_key= memcached_generate_hash(memc,
392 test_literal_param(__func__));
393 increment_request_id(&expected_ids[server_key]);
394
395 uint64_t newvalue;
396 test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc,
397 test_literal_param(__func__),
398 1, &newvalue));
399
400 return post_udp_op_check(memc, expected_ids);
401 }
402
403
404 static test_return_t udp_stat_test(memcached_st *memc)
405 {
406 memcached_return_t rc;
407 char args[]= "";
408 Expected expected_ids;
409 get_udp_request_ids(memc, expected_ids);
410 memcached_stat_st *rv= memcached_stat(memc, args, &rc);
411 memcached_stat_free(memc, rv);
412 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
413
414 return post_udp_op_check(memc, expected_ids);
415 }
416
417 static test_return_t udp_version_test(memcached_st *memc)
418 {
419 Expected expected_ids;
420 get_udp_request_ids(memc, expected_ids);
421
422 test_compare(MEMCACHED_NOT_SUPPORTED,
423 memcached_version(memc));
424
425 return post_udp_op_check(memc, expected_ids);
426 }
427
428 static test_return_t udp_get_test(memcached_st *memc)
429 {
430 memcached_return_t rc;
431 size_t vlen;
432 Expected expected_ids;
433 get_udp_request_ids(memc, expected_ids);
434 test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
435 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
436
437 return post_udp_op_check(memc, expected_ids);
438 }
439
440 static test_return_t udp_mixed_io_test(memcached_st *memc)
441 {
442 test_st mixed_io_ops [] ={
443 {"udp_set_test", 0,
444 (test_callback_fn*)udp_set_test},
445 {"udp_set_too_big_test", 0,
446 (test_callback_fn*)udp_set_too_big_test},
447 {"udp_delete_test", 0,
448 (test_callback_fn*)udp_delete_test},
449 {"udp_verbosity_test", 0,
450 (test_callback_fn*)udp_verbosity_test},
451 {"udp_quit_test", 0,
452 (test_callback_fn*)udp_quit_test},
453 #if 0
454 {"udp_flush_test", 0,
455 (test_callback_fn*)udp_flush_test},
456 #endif
457 {"udp_incr_test", 0,
458 (test_callback_fn*)udp_incr_test},
459 {"udp_decr_test", 0,
460 (test_callback_fn*)udp_decr_test},
461 {"udp_version_test", 0,
462 (test_callback_fn*)udp_version_test}
463 };
464
465 for (size_t x= 0; x < 500; x++)
466 {
467 test_st current_op= mixed_io_ops[(random() % 8)];
468 test_compare(TEST_SUCCESS, current_op.test_fn(memc));
469 }
470 return TEST_SUCCESS;
471 }
472
473 test_st udp_setup_server_tests[] ={
474 {"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
475 {"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
476 {"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
477 {0, 0, 0}
478 };
479
480 test_st upd_io_tests[] ={
481 {"udp_set_test", 0, (test_callback_fn*)udp_set_test},
482 {"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
483 {"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
484 {"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
485 {"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
486 {"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
487 {"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
488 {"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
489 {"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
490 {"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
491 {"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
492 {"udp_version_test", 0, (test_callback_fn*)udp_version_test},
493 {"udp_get_test", 0, (test_callback_fn*)udp_get_test},
494 {"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
495 {0, 0, 0}
496 };
497
498 collection_st collection[] ={
499 {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
500 {"udp_io", (test_callback_fn*)init_udp_valgrind, 0, upd_io_tests},
501 {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
502 {0, 0, 0, 0}
503 };
504
505 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
506 #include "tests/libmemcached_world.h"
507
508 void get_world(Framework *world)
509 {
510 world->collections= collection;
511
512 world->_create= (test_callback_create_fn*)world_create;
513 world->_destroy= (test_callback_destroy_fn*)world_destroy;
514
515 world->item._startup= (test_callback_fn*)world_test_startup;
516 world->item._flush= (test_callback_fn*)world_flush;
517 world->item.set_pre((test_callback_fn*)world_pre_run);
518 world->item.set_post((test_callback_fn*)world_post_run);
519 world->_on_error= (test_callback_error_fn*)world_on_error;
520
521 world->collection_startup= (test_callback_fn*)world_container_startup;
522 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
523
524 world->set_runner(&defualt_libmemcached_runner);
525 }