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