Merge in fixes for UDP
[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 <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 static void increment_request_id(uint16_t *id)
63 {
64 (*id)++;
65 if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
66 {
67 *id= 0;
68 }
69 }
70
71 static uint16_t *get_udp_request_ids(memcached_st *memc)
72 {
73 uint16_t *ids= (uint16_t*)malloc(sizeof(uint16_t) * memcached_server_count(memc));
74 assert(ids);
75
76 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
77 {
78 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
79
80 ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer);
81 }
82
83 return ids;
84 }
85
86 static test_return_t post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
87 {
88 (void)memc;
89 (void)expected_req_ids;
90 #if 0
91 memcached_server_st *cur_server = memcached_server_list(memc);
92 uint16_t *cur_req_ids = get_udp_request_ids(memc);
93
94 for (size_t x= 0; x < memcached_server_count(memc); x++)
95 {
96 test_true(cur_server[x].cursor_active == 0);
97 test_true(cur_req_ids[x] == expected_req_ids[x]);
98 }
99 free(expected_req_ids);
100 free(cur_req_ids);
101
102 #endif
103 return TEST_SUCCESS;
104 }
105
106 /*
107 ** There is a little bit of a hack here, instead of removing
108 ** the servers, I just set num host to 0 and them add then new udp servers
109 **/
110 static test_return_t init_udp(memcached_st *memc)
111 {
112 test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
113
114 return TEST_SUCCESS;
115 }
116
117 static test_return_t binary_init_udp(memcached_st *memc)
118 {
119 test_skip(TEST_SUCCESS, pre_binary(memc));
120
121 return init_udp(memc);
122 }
123
124 /* Make sure that I cant add a tcp server to a udp client */
125 static test_return_t add_tcp_server_udp_client_test(memcached_st *memc)
126 {
127 (void)memc;
128 #if 0
129 memcached_server_st server;
130 memcached_server_instance_st instance=
131 memcached_server_instance_by_position(memc, 0);
132 memcached_server_clone(&server, &memc->hosts[0]);
133 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
134 test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
135 #endif
136 return TEST_SUCCESS;
137 }
138
139 /* Make sure that I cant add a udp server to a tcp client */
140 static test_return_t add_udp_server_tcp_client_test(memcached_st *memc)
141 {
142 (void)memc;
143 #if 0
144 memcached_server_st server;
145 memcached_server_instance_st instance=
146 memcached_server_instance_by_position(memc, 0);
147 memcached_server_clone(&server, &memc->hosts[0]);
148 test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
149
150 memcached_st tcp_client;
151 memcached_create(&tcp_client);
152 test_true(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
153 #endif
154
155 return TEST_SUCCESS;
156 }
157
158 static test_return_t set_udp_behavior_test(memcached_st *memc)
159 {
160 memcached_quit(memc);
161
162 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution));
163 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
164 test_true(memc->flags.use_udp);
165 test_true(memc->flags.no_reply);
166
167 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, false));
168 test_false(memc->flags.use_udp);
169 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, false));
170 test_false(memc->flags.no_reply);
171
172 return TEST_SUCCESS;
173 }
174
175 static test_return_t udp_set_test(memcached_st *memc)
176 {
177 unsigned int num_iters= 1025; //request id rolls over at 1024
178
179 for (size_t x= 0; x < num_iters;x++)
180 {
181 uint16_t *expected_ids= get_udp_request_ids(memc);
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
195 instance->write_buffer_offset < init_offset)
196 increment_request_id(&expected_ids[server_key]);
197
198 if (rc == MEMCACHED_SUCCESS)
199 {
200 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
201 }
202 else
203 {
204 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
205 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
206 }
207 test_true(post_udp_op_check(memc, expected_ids) == TEST_SUCCESS);
208 }
209
210 return TEST_SUCCESS;
211 }
212
213 static test_return_t udp_buffered_set_test(memcached_st *memc)
214 {
215 test_compare(MEMCACHED_INVALID_ARGUMENTS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
216 return TEST_SUCCESS;
217 }
218
219 static test_return_t udp_set_too_big_test(memcached_st *memc)
220 {
221 char value[MAX_UDP_DATAGRAM_LENGTH];
222 uint16_t *expected_ids= get_udp_request_ids(memc);
223
224 memset(value, int('f'), sizeof(value));
225
226 test_compare_hint(MEMCACHED_WRITE_FAILURE, memcached_set(memc, test_literal_param("bar"), value, sizeof(value), time_t(0), uint32_t(0)),
227 memcached_last_error_message(memc));
228
229 return post_udp_op_check(memc,expected_ids);
230 }
231
232 static test_return_t udp_delete_test(memcached_st *memc)
233 {
234 //request id rolls over at 1024
235 for (size_t x= 0; x < 1025; x++)
236 {
237 uint16_t *expected_ids=get_udp_request_ids(memc);
238 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
239 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
240 size_t init_offset= instance->write_buffer_offset;
241
242 memcached_return_t rc= memcached_delete(memc, test_literal_param("foo"), 0);
243 test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED);
244
245 if (rc == MEMCACHED_SUCCESS or instance->write_buffer_offset < init_offset)
246 {
247 increment_request_id(&expected_ids[server_key]);
248 }
249
250 if (rc == MEMCACHED_SUCCESS)
251 {
252 test_true(instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
253 }
254 else
255 {
256 test_true(instance->write_buffer_offset != UDP_DATAGRAM_HEADER_LENGTH);
257 test_true(instance->write_buffer_offset <= MAX_UDP_DATAGRAM_LENGTH);
258 }
259 test_true(post_udp_op_check(memc,expected_ids) == TEST_SUCCESS);
260 }
261 return TEST_SUCCESS;
262 }
263
264 static test_return_t udp_buffered_delete_test(memcached_st *memc)
265 {
266 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
267 return udp_delete_test(memc);
268 }
269
270 static test_return_t udp_verbosity_test(memcached_st *memc)
271 {
272 uint16_t *expected_ids= get_udp_request_ids(memc);
273
274 for (size_t x= 0; x < memcached_server_count(memc); x++)
275 {
276 increment_request_id(&expected_ids[x]);
277 }
278
279 test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
280
281 return post_udp_op_check(memc,expected_ids);
282 }
283
284 static test_return_t udp_quit_test(memcached_st *memc)
285 {
286 uint16_t *expected_ids= get_udp_request_ids(memc);
287 memcached_quit(memc);
288 return post_udp_op_check(memc, expected_ids);
289 }
290
291 static test_return_t udp_flush_test(memcached_st *memc)
292 {
293 uint16_t *expected_ids= get_udp_request_ids(memc);
294
295 for (size_t x= 0; x < memcached_server_count(memc); x++)
296 {
297 increment_request_id(&expected_ids[x]);
298 }
299 test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
300
301 return post_udp_op_check(memc, expected_ids);
302 }
303
304 static test_return_t udp_incr_test(memcached_st *memc)
305 {
306 test_compare(MEMCACHED_SUCCESS, memcached_set(memc, test_literal_param("incr"),
307 test_literal_param("1"),
308 (time_t)0, (uint32_t)0));
309
310 uint16_t *expected_ids= get_udp_request_ids(memc);
311 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
312 increment_request_id(&expected_ids[server_key]);
313
314 uint64_t newvalue;
315 test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
316
317 return post_udp_op_check(memc, expected_ids);
318 }
319
320 static test_return_t udp_decr_test(memcached_st *memc)
321 {
322 test_compare(MEMCACHED_SUCCESS, memcached_set(memc,
323 test_literal_param("decr"),
324 test_literal_param("1"),
325 (time_t)0, (uint32_t)0));
326
327 uint16_t *expected_ids= get_udp_request_ids(memc);
328 unsigned int server_key= memcached_generate_hash(memc, test_literal_param("decr"));
329 increment_request_id(&expected_ids[server_key]);
330
331 uint64_t newvalue;
332 test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc, test_literal_param("decr"), 1, &newvalue));
333
334 return post_udp_op_check(memc, expected_ids);
335 }
336
337
338 static test_return_t udp_stat_test(memcached_st *memc)
339 {
340 memcached_return_t rc;
341 char args[]= "";
342 uint16_t *expected_ids = get_udp_request_ids(memc);
343 memcached_stat_st *rv = memcached_stat(memc, args, &rc);
344 free(rv);
345 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
346
347 return post_udp_op_check(memc, expected_ids);
348 }
349
350 static test_return_t udp_version_test(memcached_st *memc)
351 {
352 uint16_t *expected_ids = get_udp_request_ids(memc);
353
354 test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
355
356 return post_udp_op_check(memc, expected_ids);
357 }
358
359 static test_return_t udp_get_test(memcached_st *memc)
360 {
361 memcached_return_t rc;
362 size_t vlen;
363 uint16_t *expected_ids = get_udp_request_ids(memc);
364 test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
365 test_compare(MEMCACHED_NOT_SUPPORTED, rc);
366
367 return post_udp_op_check(memc, expected_ids);
368 }
369
370 static test_return_t udp_mixed_io_test(memcached_st *memc)
371 {
372 test_st mixed_io_ops [] ={
373 {"udp_set_test", 0,
374 (test_callback_fn*)udp_set_test},
375 {"udp_set_too_big_test", 0,
376 (test_callback_fn*)udp_set_too_big_test},
377 {"udp_delete_test", 0,
378 (test_callback_fn*)udp_delete_test},
379 {"udp_verbosity_test", 0,
380 (test_callback_fn*)udp_verbosity_test},
381 {"udp_quit_test", 0,
382 (test_callback_fn*)udp_quit_test},
383 #if 0
384 {"udp_flush_test", 0,
385 (test_callback_fn*)udp_flush_test},
386 #endif
387 {"udp_incr_test", 0,
388 (test_callback_fn*)udp_incr_test},
389 {"udp_decr_test", 0,
390 (test_callback_fn*)udp_decr_test},
391 {"udp_version_test", 0,
392 (test_callback_fn*)udp_version_test}
393 };
394
395 for (size_t x= 0; x < 500; x++)
396 {
397 test_st current_op= mixed_io_ops[(random() % 8)];
398 test_compare(TEST_SUCCESS, current_op.test_fn(memc));
399 }
400 return TEST_SUCCESS;
401 }
402
403 test_st udp_setup_server_tests[] ={
404 {"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
405 {"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
406 {"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
407 {0, 0, 0}
408 };
409
410 test_st upd_io_tests[] ={
411 {"udp_set_test", 0, (test_callback_fn*)udp_set_test},
412 {"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
413 {"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
414 {"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
415 {"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
416 {"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
417 {"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
418 {"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
419 {"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
420 {"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
421 {"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
422 {"udp_version_test", 0, (test_callback_fn*)udp_version_test},
423 {"udp_get_test", 0, (test_callback_fn*)udp_get_test},
424 {"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
425 {0, 0, 0}
426 };
427
428 collection_st collection[] ={
429 {"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
430 {"udp_io", (test_callback_fn*)init_udp, 0, upd_io_tests},
431 {"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
432 {0, 0, 0, 0}
433 };
434
435 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
436 #include "libmemcached_world.h"
437
438 void get_world(Framework *world)
439 {
440 world->collections= collection;
441
442 world->_create= (test_callback_create_fn*)world_create;
443 world->_destroy= (test_callback_destroy_fn*)world_destroy;
444
445 world->item._startup= (test_callback_fn*)world_test_startup;
446 world->item._flush= (test_callback_fn*)world_flush;
447 world->item.set_pre((test_callback_fn*)world_pre_run);
448 world->item.set_post((test_callback_fn*)world_post_run);
449 world->_on_error= (test_callback_error_fn*)world_on_error;
450
451 world->collection_startup= (test_callback_fn*)world_container_startup;
452 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
453
454 world->set_runner(&defualt_libmemcached_runner);
455 }