1 /* libMemcached Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
13 #include <libtest/common.h>
15 #include <libmemcached/common.h>
23 #include <sys/types.h>
29 #include <libtest/server.h>
31 #define SERVERS_TO_CREATE 5
33 #ifndef __INTEL_COMPILER
34 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
38 @note This should be testing to see if the server really supports the binary protocol.
40 static test_return_t
pre_binary(memcached_st
*memc
)
42 memcached_return_t rc
= MEMCACHED_FAILURE
;
43 memcached_st
*memc_clone
;
44 memcached_server_instance_st instance
;
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
);
52 instance
= memcached_server_instance_by_position(memc_clone
, 0);
54 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
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);
61 memcached_free(memc_clone
);
63 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
66 static void increment_request_id(uint16_t *id
)
69 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
73 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
75 uint16_t *ids
= (uint16_t*)malloc(sizeof(uint16_t) * memcached_server_count(memc
));
78 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
80 memcached_server_instance_st instance
=
81 memcached_server_instance_by_position(memc
, x
);
83 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
);
89 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
92 (void)expected_req_ids
;
94 memcached_server_st
*cur_server
= memcached_server_list(memc
);
95 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
97 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
99 test_true(cur_server
[x
].cursor_active
== 0);
100 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
102 free(expected_req_ids
);
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
113 static test_return_t
init_udp(memcached_st
*memc
)
115 memcached_version(memc
);
117 memcached_server_instance_st instance
=
118 memcached_server_instance_by_position(memc
, 0);
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)
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
++)
130 memcached_server_instance_st set_instance
=
131 memcached_server_instance_by_position(memc
, x
);
133 memcached_server_free(((memcached_server_write_instance_st
)set_instance
));
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
++)
140 memcached_server_instance_st set_instance
=
141 memcached_server_instance_by_position(memc
, x
);
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
);
151 static test_return_t
binary_init_udp(memcached_st
*memc
)
153 test_return_t test_rc
;
154 test_rc
= pre_binary(memc
);
156 if (test_rc
!= TEST_SUCCESS
)
159 return init_udp(memc
);
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
)
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
);
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
)
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
);
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
);
196 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
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
);
206 test_true(memcached_server_count(memc
) == 0);
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
));
216 static test_return_t
udp_set_test(memcached_st
*memc
)
218 unsigned int num_iters
= 1025; //request id rolls over at 1024
220 for (size_t x
= 0; x
< num_iters
;x
++)
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
;
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
239 if (rc
== MEMCACHED_SUCCESS
||
240 instance
->write_buffer_offset
< init_offset
)
241 increment_request_id(&expected_ids
[server_key
]);
243 if (rc
== MEMCACHED_SUCCESS
)
245 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
249 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
250 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
252 test_true(post_udp_op_check(memc
, expected_ids
) == TEST_SUCCESS
);
257 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
259 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
260 return udp_set_test(memc
);
263 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
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
);
274 return post_udp_op_check(memc
,expected_ids
);
277 static test_return_t
udp_delete_test(memcached_st
*memc
)
279 unsigned int num_iters
= 1025; //request id rolls over at 1024
281 for (size_t x
= 0; x
< num_iters
;x
++)
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
;
291 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
292 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
294 if (rc
== MEMCACHED_SUCCESS
|| instance
->write_buffer_offset
< init_offset
)
295 increment_request_id(&expected_ids
[server_key
]);
296 if (rc
== MEMCACHED_SUCCESS
)
298 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
302 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
303 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
305 test_true(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
310 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
312 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
313 return udp_delete_test(memc
);
316 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
318 memcached_return_t rc
;
319 uint16_t *expected_ids
= get_udp_request_ids(memc
);
321 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
323 increment_request_id(&expected_ids
[x
]);
326 rc
= memcached_verbosity(memc
,3);
327 test_true(rc
== MEMCACHED_SUCCESS
);
328 return post_udp_op_check(memc
,expected_ids
);
331 static test_return_t
udp_quit_test(memcached_st
*memc
)
333 uint16_t *expected_ids
= get_udp_request_ids(memc
);
334 memcached_quit(memc
);
335 return post_udp_op_check(memc
, expected_ids
);
338 static test_return_t
udp_flush_test(memcached_st
*memc
)
340 memcached_return_t rc
;
341 uint16_t *expected_ids
= get_udp_request_ids(memc
);
343 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
345 increment_request_id(&expected_ids
[x
]);
348 rc
= memcached_flush(memc
,0);
349 test_true(rc
== MEMCACHED_SUCCESS
);
350 return post_udp_op_check(memc
,expected_ids
);
353 static test_return_t
udp_incr_test(memcached_st
*memc
)
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);
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
]);
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
);
372 static test_return_t
udp_decr_test(memcached_st
*memc
)
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);
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
]);
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
);
392 static test_return_t
udp_stat_test(memcached_st
*memc
)
394 memcached_stat_st
* rv
= NULL
;
395 memcached_return_t rc
;
397 uint16_t *expected_ids
= get_udp_request_ids(memc
);
398 rv
= memcached_stat(memc
, args
, &rc
);
400 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
401 return post_udp_op_check(memc
, expected_ids
);
404 static test_return_t
udp_version_test(memcached_st
*memc
)
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
);
413 static test_return_t
udp_get_test(memcached_st
*memc
)
415 memcached_return_t rc
;
416 const char *key
= "foo";
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
);
425 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
428 test_st mixed_io_ops
[] ={
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
},
438 (test_callback_fn
*)udp_quit_test
},
439 {"udp_flush_test", 0,
440 (test_callback_fn
*)udp_flush_test
},
442 (test_callback_fn
*)udp_incr_test
},
444 (test_callback_fn
*)udp_decr_test
},
445 {"udp_version_test", 0,
446 (test_callback_fn
*)udp_version_test
}
449 for (size_t x
= 0; x
< 500; x
++)
451 current_op
= mixed_io_ops
[random() % 9];
452 test_true(current_op
.test_fn(memc
) == TEST_SUCCESS
);
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
},
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
},
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
},
489 #define SERVERS_TO_CREATE 5
491 #include "libmemcached_world.h"
493 void get_world(Framework
*world
)
495 world
->collections
= collection
;
497 world
->_create
= (test_callback_create_fn
*)world_create
;
498 world
->_destroy
= (test_callback_fn
*)world_destroy
;
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
;
506 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
507 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
509 world
->runner
= &defualt_libmemcached_runner
;