Merge in pid/ping status.
[awesomized/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 <libtest/common.h>
14
15 #include <libmemcached/common.h>
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <time.h>
28
29 #include <libtest/server.h>
30
31 #define SERVERS_TO_CREATE 5
32
33 #ifndef __INTEL_COMPILER
34 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
35 #endif
36
37 /**
38 @note This should be testing to see if the server really supports the binary protocol.
39 */
40 static test_return_t pre_binary(memcached_st *memc)
41 {
42 memcached_return_t rc= MEMCACHED_FAILURE;
43 memcached_st *memc_clone;
44 memcached_server_instance_st instance;
45
46 memc_clone= memcached_clone(NULL, memc);
47 test_true(memc_clone);
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 instance= memcached_server_instance_by_position(memc_clone, 0);
53
54 if (instance->major_version >= 1 && instance->minor_version > 2)
55 {
56 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
57 test_true(rc == MEMCACHED_SUCCESS);
58 test_true(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
59 }
60
61 memcached_free(memc_clone);
62
63 return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED;
64 }
65
66 static void increment_request_id(uint16_t *id)
67 {
68 (*id)++;
69 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
70 *id= 0;
71 }
72
73 static uint16_t *get_udp_request_ids(memcached_st *memc)
74 {
75 uint16_t *ids= (uint16_t*)malloc(sizeof(uint16_t) * memcached_server_count(memc));
76 assert(ids);
77
78 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
79 {
80 memcached_server_instance_st instance=
81 memcached_server_instance_by_position(memc, x);
82
83 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer);
84 }
85
86 return ids;
87 }
88
89 static test_return_t post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
90 {
91 (void)memc;
92 (void)expected_req_ids;
93 #if 0
94 memcached_server_st *cur_server = memcached_server_list(memc);
95 uint16_t *cur_req_ids = get_udp_request_ids(memc);
96
97 for (size_t x= 0; x < memcached_server_count(memc); x++)
98 {
99 test_true(cur_server[x].cursor_active == 0);
100 test_true(cur_req_ids[x] == expected_req_ids[x]);
101 }
102 free(expected_req_ids);
103 free(cur_req_ids);
104
105 #endif
106 return TEST_SUCCESS;
107 }
108
109 /*
110 ** There is a little bit of a hack here, instead of removing
111 ** the servers, I just set num host to 0 and them add then new udp servers
112 **/
113 static test_return_t init_udp(memcached_st *memc)
114 {
115 memcached_version(memc);
116 #if 0
117 memcached_server_instance_st instance=
118 memcached_server_instance_by_position(memc, 0);
119
120 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
121 if (instance->major_version != 1 || instance->minor_version != 2
122 || instance->micro_version < 6)
123 return TEST_SKIPPED;
124
125 uint32_t num_hosts= memcached_server_count(memc);
126 memcached_server_st servers[num_hosts];
127 memcpy(servers, memcached_server_list(memc), sizeof(memcached_server_st) * num_hosts);
128 for (uint32_t x= 0; x < num_hosts; x++)
129 {
130 memcached_server_instance_st set_instance=
131 memcached_server_instance_by_position(memc, x);
132
133 memcached_server_free(((memcached_server_write_instance_st)set_instance));
134 }
135
136 memc->number_of_hosts= 0;
137 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
138 for (uint32_t x= 0; x < num_hosts; x++)
139 {
140 memcached_server_instance_st set_instance=
141 memcached_server_instance_by_position(memc, x);
142
143 test_true(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
144 test_true(set_instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
145 }
146 #endif
147
148 return TEST_SKIPPED;
149 }
150
151 static test_return_t binary_init_udp(memcached_st *memc)
152 {
153 test_return_t test_rc;
154 test_rc= pre_binary(memc);
155
156 if (test_rc != TEST_SUCCESS)
157 return test_rc;
158
159 return init_udp(memc);
160 }
161
162 /* Make sure that I cant add a tcp server to a udp client */
163 static test_return_t add_tcp_server_udp_client_test(memcached_st *memc)
164 {
165 (void)memc;
166 #if 0
167 memcached_server_st server;
168 memcached_server_instance_st instance=
169 memcached_server_instance_by_position(memc, 0);
170 memcached_server_clone(&server, &memc->hosts[0]);
171 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
172 test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
173 #endif
174 return TEST_SUCCESS;
175 }
176
177 /* Make sure that I cant add a udp server to a tcp client */
178 static test_return_t add_udp_server_tcp_client_test(memcached_st *memc)
179 {
180 (void)memc;
181 #if 0
182 memcached_server_st server;
183 memcached_server_instance_st instance=
184 memcached_server_instance_by_position(memc, 0);
185 memcached_server_clone(&server, &memc->hosts[0]);
186 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
187
188 memcached_st tcp_client;
189 memcached_create(&tcp_client);
190 test_true(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
191 #endif
192
193 return TEST_SUCCESS;
194 }
195
196 static test_return_t set_udp_behavior_test(memcached_st *memc)
197 {
198
199 memcached_quit(memc);
200 memc->number_of_hosts= 0;
201 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution);
202 test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1) == MEMCACHED_SUCCESS);
203 test_true(memc->flags.use_udp);
204 test_true(memc->flags.no_reply);
205
206 test_true(memcached_server_count(memc) == 0);
207
208 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0);
209 test_true(! (memc->flags.use_udp));
210 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY,0);
211 test_true(! (memc->flags.no_reply));
212
213 return TEST_SUCCESS;
214 }
215
216 static test_return_t udp_set_test(memcached_st *memc)
217 {
218 unsigned int num_iters= 1025; //request id rolls over at 1024
219
220 for (size_t x= 0; x < num_iters;x++)
221 {
222 memcached_return_t rc;
223 const char *key= "foo";
224 const char *value= "when we sanitize";
225 uint16_t *expected_ids= get_udp_request_ids(memc);
226 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
227 memcached_server_instance_st instance=
228 memcached_server_instance_by_position(memc, server_key);
229 size_t init_offset= instance->write_buffer_offset;
230
231 rc= memcached_set(memc, key, strlen(key),
232 value, strlen(value),
233 (time_t)0, (uint32_t)0);
234 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
235 /** NB, the check below assumes that if new write_ptr is less than
236 * the original write_ptr that we have flushed. For large payloads, this
237 * maybe an invalid assumption, but for the small payload we have it is OK
238 */
239 if (rc == MEMCACHED_SUCCESS ||
240 instance->write_buffer_offset < init_offset)
241 increment_request_id(&expected_ids[server_key]);
242
243 if (rc == MEMCACHED_SUCCESS)
244 {
245 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
246 }
247 else
248 {
249 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
250 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
251 }
252 test_true(post_udp_op_check(memc, expected_ids) == TEST_SUCCESS);
253 }
254 return TEST_SUCCESS;
255 }
256
257 static test_return_t udp_buffered_set_test(memcached_st *memc)
258 {
259 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
260 return udp_set_test(memc);
261 }
262
263 static test_return_t udp_set_too_big_test(memcached_st *memc)
264 {
265 memcached_return_t rc;
266 const char *key= "bar";
267 char value[MAX_UDP_DATAGRAM_LENGTH];
268 uint16_t *expected_ids= get_udp_request_ids(memc);
269 rc= memcached_set(memc, key, strlen(key),
270 value, MAX_UDP_DATAGRAM_LENGTH,
271 (time_t)0, (uint32_t)0);
272 test_true(rc == MEMCACHED_WRITE_FAILURE);
273
274 return post_udp_op_check(memc,expected_ids);
275 }
276
277 static test_return_t udp_delete_test(memcached_st *memc)
278 {
279 unsigned int num_iters= 1025; //request id rolls over at 1024
280
281 for (size_t x= 0; x < num_iters;x++)
282 {
283 memcached_return_t rc;
284 const char *key= "foo";
285 uint16_t *expected_ids=get_udp_request_ids(memc);
286 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
287 memcached_server_instance_st instance=
288 memcached_server_instance_by_position(memc, server_key);
289 size_t init_offset= instance->write_buffer_offset;
290
291 rc= memcached_delete(memc, key, strlen(key), 0);
292 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
293
294 if (rc == MEMCACHED_SUCCESS || instance->write_buffer_offset < init_offset)
295 increment_request_id(&expected_ids[server_key]);
296 if (rc == MEMCACHED_SUCCESS)
297 {
298 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
299 }
300 else
301 {
302 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
303 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
304 }
305 test_true(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
306 }
307 return TEST_SUCCESS;
308 }
309
310 static test_return_t udp_buffered_delete_test(memcached_st *memc)
311 {
312 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
313 return udp_delete_test(memc);
314 }
315
316 static test_return_t udp_verbosity_test(memcached_st *memc)
317 {
318 memcached_return_t rc;
319 uint16_t *expected_ids= get_udp_request_ids(memc);
320
321 for (size_t x= 0; x < memcached_server_count(memc); x++)
322 {
323 increment_request_id(&expected_ids[x]);
324 }
325
326 rc= memcached_verbosity(memc,3);
327 test_true(rc == MEMCACHED_SUCCESS);
328 return post_udp_op_check(memc,expected_ids);
329 }
330
331 static test_return_t udp_quit_test(memcached_st *memc)
332 {
333 uint16_t *expected_ids= get_udp_request_ids(memc);
334 memcached_quit(memc);
335 return post_udp_op_check(memc, expected_ids);
336 }
337
338 static test_return_t udp_flush_test(memcached_st *memc)
339 {
340 memcached_return_t rc;
341 uint16_t *expected_ids= get_udp_request_ids(memc);
342
343 for (size_t x= 0; x < memcached_server_count(memc); x++)
344 {
345 increment_request_id(&expected_ids[x]);
346 }
347
348 rc= memcached_flush(memc,0);
349 test_true(rc == MEMCACHED_SUCCESS);
350 return post_udp_op_check(memc,expected_ids);
351 }
352
353 static test_return_t udp_incr_test(memcached_st *memc)
354 {
355 memcached_return_t rc;
356 const char *key= "incr";
357 const char *value= "1";
358 rc= memcached_set(memc, key, strlen(key),
359 value, strlen(value),
360 (time_t)0, (uint32_t)0);
361
362 test_true(rc == MEMCACHED_SUCCESS);
363 uint16_t *expected_ids= get_udp_request_ids(memc);
364 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
365 increment_request_id(&expected_ids[server_key]);
366 uint64_t newvalue;
367 rc= memcached_increment(memc, key, strlen(key), 1, &newvalue);
368 test_true(rc == MEMCACHED_SUCCESS);
369 return post_udp_op_check(memc, expected_ids);
370 }
371
372 static test_return_t udp_decr_test(memcached_st *memc)
373 {
374 memcached_return_t rc;
375 const char *key= "decr";
376 const char *value= "1";
377 rc= memcached_set(memc, key, strlen(key),
378 value, strlen(value),
379 (time_t)0, (uint32_t)0);
380
381 test_true(rc == MEMCACHED_SUCCESS);
382 uint16_t *expected_ids= get_udp_request_ids(memc);
383 unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
384 increment_request_id(&expected_ids[server_key]);
385 uint64_t newvalue;
386 rc= memcached_decrement(memc, key, strlen(key), 1, &newvalue);
387 test_true(rc == MEMCACHED_SUCCESS);
388 return post_udp_op_check(memc, expected_ids);
389 }
390
391
392 static test_return_t udp_stat_test(memcached_st *memc)
393 {
394 memcached_stat_st * rv= NULL;
395 memcached_return_t rc;
396 char args[]= "";
397 uint16_t *expected_ids = get_udp_request_ids(memc);
398 rv = memcached_stat(memc, args, &rc);
399 free(rv);
400 test_true(rc == MEMCACHED_NOT_SUPPORTED);
401 return post_udp_op_check(memc, expected_ids);
402 }
403
404 static test_return_t udp_version_test(memcached_st *memc)
405 {
406 memcached_return_t rc;
407 uint16_t *expected_ids = get_udp_request_ids(memc);
408 rc = memcached_version(memc);
409 test_true(rc == MEMCACHED_NOT_SUPPORTED);
410 return post_udp_op_check(memc, expected_ids);
411 }
412
413 static test_return_t udp_get_test(memcached_st *memc)
414 {
415 memcached_return_t rc;
416 const char *key= "foo";
417 size_t vlen;
418 uint16_t *expected_ids = get_udp_request_ids(memc);
419 char *val= memcached_get(memc, key, strlen(key), &vlen, (uint32_t)0, &rc);
420 test_true(rc == MEMCACHED_NOT_SUPPORTED);
421 test_true(val == NULL);
422 return post_udp_op_check(memc, expected_ids);
423 }
424
425 static test_return_t udp_mixed_io_test(memcached_st *memc)
426 {
427 test_st current_op;
428 test_st mixed_io_ops [] ={
429 {"udp_set_test", 0,
430 (test_callback_fn*)udp_set_test},
431 {"udp_set_too_big_test", 0,
432 (test_callback_fn*)udp_set_too_big_test},
433 {"udp_delete_test", 0,
434 (test_callback_fn*)udp_delete_test},
435 {"udp_verbosity_test", 0,
436 (test_callback_fn*)udp_verbosity_test},
437 {"udp_quit_test", 0,
438 (test_callback_fn*)udp_quit_test},
439 {"udp_flush_test", 0,
440 (test_callback_fn*)udp_flush_test},
441 {"udp_incr_test", 0,
442 (test_callback_fn*)udp_incr_test},
443 {"udp_decr_test", 0,
444 (test_callback_fn*)udp_decr_test},
445 {"udp_version_test", 0,
446 (test_callback_fn*)udp_version_test}
447 };
448
449 for (size_t x= 0; x < 500; x++)
450 {
451 current_op= mixed_io_ops[random() % 9];
452 test_true(current_op.test_fn(memc) == TEST_SUCCESS);
453 }
454 return TEST_SUCCESS;
455 }
456
457 test_st udp_setup_server_tests[] ={
458 {"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
459 {"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
460 {"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
461 {0, 0, 0}
462 };
463
464 test_st upd_io_tests[] ={
465 {"udp_set_test", 0, (test_callback_fn*)udp_set_test},
466 {"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
467 {"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
468 {"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
469 {"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
470 {"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
471 {"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
472 {"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
473 {"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
474 {"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
475 {"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
476 {"udp_version_test", 0, (test_callback_fn*)udp_version_test},
477 {"udp_get_test", 0, (test_callback_fn*)udp_get_test},
478 {"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
479 {0, 0, 0}
480 };
481
482 collection_st collection[] ={
483 {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
484 {"udp_io", (test_callback_fn*)init_udp, 0, upd_io_tests},
485 {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
486 {0, 0, 0, 0}
487 };
488
489 #define SERVERS_TO_CREATE 5
490
491 #include "libmemcached_world.h"
492
493 void get_world(Framework *world)
494 {
495 world->collections= collection;
496
497 world->_create= (test_callback_create_fn*)world_create;
498 world->_destroy= (test_callback_fn*)world_destroy;
499
500 world->item._startup= (test_callback_fn*)world_test_startup;
501 world->item._flush= (test_callback_fn*)world_flush;
502 world->item.set_pre((test_callback_fn*)world_pre_run);
503 world->item.set_post((test_callback_fn*)world_post_run);
504 world->_on_error= (test_callback_error_fn*)world_on_error;
505
506 world->collection_startup= (test_callback_fn*)world_container_startup;
507 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
508
509 world->runner= &defualt_libmemcached_runner;
510 }