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