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