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.
15 #include "libmemcached/common.h"
23 #include <sys/types.h>
33 #define SERVERS_TO_CREATE 5
37 @note This should be testing to see if the server really supports the binary protocol.
39 static test_return_t
pre_binary(memcached_st
*memc
)
41 memcached_return_t rc
= MEMCACHED_FAILURE
;
42 memcached_st
*memc_clone
;
43 memcached_server_instance_st instance
;
45 memc_clone
= memcached_clone(NULL
, memc
);
46 test_true(memc_clone
);
47 // The memcached_version needs to be done on a clone, because the server
48 // will not toggle protocol on an connection.
49 memcached_version(memc_clone
);
51 instance
= memcached_server_instance_by_position(memc_clone
, 0);
53 if (instance
->major_version
>= 1 && instance
->minor_version
> 2)
55 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1);
56 test_true(rc
== MEMCACHED_SUCCESS
);
57 test_true(memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
) == 1);
60 memcached_free(memc_clone
);
62 return rc
== MEMCACHED_SUCCESS
? TEST_SUCCESS
: TEST_SKIPPED
;
65 static void increment_request_id(uint16_t *id
)
68 if ((*id
& UDP_REQUEST_ID_THREAD_MASK
) != 0)
72 static uint16_t *get_udp_request_ids(memcached_st
*memc
)
74 uint16_t *ids
= malloc(sizeof(uint16_t) * memcached_server_count(memc
));
77 for (uint32_t x
= 0; x
< memcached_server_count(memc
); x
++)
79 memcached_server_instance_st instance
=
80 memcached_server_instance_by_position(memc
, x
);
82 ids
[x
]= get_udp_datagram_request_id((struct udp_datagram_header_st
*) ((memcached_server_instance_st
)instance
)->write_buffer
);
88 static test_return_t
post_udp_op_check(memcached_st
*memc
, uint16_t *expected_req_ids
)
91 (void)expected_req_ids
;
93 memcached_server_st
*cur_server
= memcached_server_list(memc
);
94 uint16_t *cur_req_ids
= get_udp_request_ids(memc
);
96 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
98 test_true(cur_server
[x
].cursor_active
== 0);
99 test_true(cur_req_ids
[x
] == expected_req_ids
[x
]);
101 free(expected_req_ids
);
109 ** There is a little bit of a hack here, instead of removing
110 ** the servers, I just set num host to 0 and them add then new udp servers
112 static test_return_t
init_udp(memcached_st
*memc
)
114 memcached_version(memc
);
116 memcached_server_instance_st instance
=
117 memcached_server_instance_by_position(memc
, 0);
119 /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
120 if (instance
->major_version
!= 1 || instance
->minor_version
!= 2
121 || instance
->micro_version
< 6)
124 uint32_t num_hosts
= memcached_server_count(memc
);
125 memcached_server_st servers
[num_hosts
];
126 memcpy(servers
, memcached_server_list(memc
), sizeof(memcached_server_st
) * num_hosts
);
127 for (uint32_t x
= 0; x
< num_hosts
; x
++)
129 memcached_server_instance_st set_instance
=
130 memcached_server_instance_by_position(memc
, x
);
132 memcached_server_free(((memcached_server_write_instance_st
)set_instance
));
135 memc
->number_of_hosts
= 0;
136 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1);
137 for (uint32_t x
= 0; x
< num_hosts
; x
++)
139 memcached_server_instance_st set_instance
=
140 memcached_server_instance_by_position(memc
, x
);
142 test_true(memcached_server_add_udp(memc
, servers
[x
].hostname
, servers
[x
].port
) == MEMCACHED_SUCCESS
);
143 test_true(set_instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
150 static test_return_t
binary_init_udp(memcached_st
*memc
)
152 test_return_t test_rc
;
153 test_rc
= pre_binary(memc
);
155 if (test_rc
!= TEST_SUCCESS
)
158 return init_udp(memc
);
161 /* Make sure that I cant add a tcp server to a udp client */
162 static test_return_t
add_tcp_server_udp_client_test(memcached_st
*memc
)
166 memcached_server_st server
;
167 memcached_server_instance_st instance
=
168 memcached_server_instance_by_position(memc
, 0);
169 memcached_server_clone(&server
, &memc
->hosts
[0]);
170 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
171 test_true(memcached_server_add(memc
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
176 /* Make sure that I cant add a udp server to a tcp client */
177 static test_return_t
add_udp_server_tcp_client_test(memcached_st
*memc
)
181 memcached_server_st server
;
182 memcached_server_instance_st instance
=
183 memcached_server_instance_by_position(memc
, 0);
184 memcached_server_clone(&server
, &memc
->hosts
[0]);
185 test_true(memcached_server_remove(&(memc
->hosts
[0])) == MEMCACHED_SUCCESS
);
187 memcached_st tcp_client
;
188 memcached_create(&tcp_client
);
189 test_true(memcached_server_add_udp(&tcp_client
, server
.hostname
, server
.port
) == MEMCACHED_INVALID_HOST_PROTOCOL
);
195 static test_return_t
set_udp_behavior_test(memcached_st
*memc
)
198 memcached_quit(memc
);
199 memc
->number_of_hosts
= 0;
200 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_DISTRIBUTION
, memc
->distribution
);
201 test_true(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, 1) == MEMCACHED_SUCCESS
);
202 test_true(memc
->flags
.use_udp
);
203 test_true(memc
->flags
.no_reply
);
205 test_true(memcached_server_count(memc
) == 0);
207 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,0);
208 test_true(! (memc
->flags
.use_udp
));
209 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NOREPLY
,0);
210 test_true(! (memc
->flags
.no_reply
));
215 static test_return_t
udp_set_test(memcached_st
*memc
)
217 unsigned int num_iters
= 1025; //request id rolls over at 1024
219 for (size_t x
= 0; x
< num_iters
;x
++)
221 memcached_return_t rc
;
222 const char *key
= "foo";
223 const char *value
= "when we sanitize";
224 uint16_t *expected_ids
= get_udp_request_ids(memc
);
225 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
226 memcached_server_instance_st instance
=
227 memcached_server_instance_by_position(memc
, server_key
);
228 size_t init_offset
= instance
->write_buffer_offset
;
230 rc
= memcached_set(memc
, key
, strlen(key
),
231 value
, strlen(value
),
232 (time_t)0, (uint32_t)0);
233 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
234 /** NB, the check below assumes that if new write_ptr is less than
235 * the original write_ptr that we have flushed. For large payloads, this
236 * maybe an invalid assumption, but for the small payload we have it is OK
238 if (rc
== MEMCACHED_SUCCESS
||
239 instance
->write_buffer_offset
< init_offset
)
240 increment_request_id(&expected_ids
[server_key
]);
242 if (rc
== MEMCACHED_SUCCESS
)
244 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
248 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
249 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
251 test_true(post_udp_op_check(memc
, expected_ids
) == TEST_SUCCESS
);
256 static test_return_t
udp_buffered_set_test(memcached_st
*memc
)
258 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
259 return udp_set_test(memc
);
262 static test_return_t
udp_set_too_big_test(memcached_st
*memc
)
264 memcached_return_t rc
;
265 const char *key
= "bar";
266 char value
[MAX_UDP_DATAGRAM_LENGTH
];
267 uint16_t *expected_ids
= get_udp_request_ids(memc
);
268 rc
= memcached_set(memc
, key
, strlen(key
),
269 value
, MAX_UDP_DATAGRAM_LENGTH
,
270 (time_t)0, (uint32_t)0);
271 test_true(rc
== MEMCACHED_WRITE_FAILURE
);
273 return post_udp_op_check(memc
,expected_ids
);
276 static test_return_t
udp_delete_test(memcached_st
*memc
)
278 unsigned int num_iters
= 1025; //request id rolls over at 1024
280 for (size_t x
= 0; x
< num_iters
;x
++)
282 memcached_return_t rc
;
283 const char *key
= "foo";
284 uint16_t *expected_ids
=get_udp_request_ids(memc
);
285 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
286 memcached_server_instance_st instance
=
287 memcached_server_instance_by_position(memc
, server_key
);
288 size_t init_offset
= instance
->write_buffer_offset
;
290 rc
= memcached_delete(memc
, key
, strlen(key
), 0);
291 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
293 if (rc
== MEMCACHED_SUCCESS
|| instance
->write_buffer_offset
< init_offset
)
294 increment_request_id(&expected_ids
[server_key
]);
295 if (rc
== MEMCACHED_SUCCESS
)
297 test_true(instance
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
);
301 test_true(instance
->write_buffer_offset
!= UDP_DATAGRAM_HEADER_LENGTH
);
302 test_true(instance
->write_buffer_offset
<= MAX_UDP_DATAGRAM_LENGTH
);
304 test_true(post_udp_op_check(memc
,expected_ids
) == TEST_SUCCESS
);
309 static test_return_t
udp_buffered_delete_test(memcached_st
*memc
)
311 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
312 return udp_delete_test(memc
);
315 static test_return_t
udp_verbosity_test(memcached_st
*memc
)
317 memcached_return_t rc
;
318 uint16_t *expected_ids
= get_udp_request_ids(memc
);
320 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
322 increment_request_id(&expected_ids
[x
]);
325 rc
= memcached_verbosity(memc
,3);
326 test_true(rc
== MEMCACHED_SUCCESS
);
327 return post_udp_op_check(memc
,expected_ids
);
330 static test_return_t
udp_quit_test(memcached_st
*memc
)
332 uint16_t *expected_ids
= get_udp_request_ids(memc
);
333 memcached_quit(memc
);
334 return post_udp_op_check(memc
, expected_ids
);
337 static test_return_t
udp_flush_test(memcached_st
*memc
)
339 memcached_return_t rc
;
340 uint16_t *expected_ids
= get_udp_request_ids(memc
);
342 for (size_t x
= 0; x
< memcached_server_count(memc
); x
++)
344 increment_request_id(&expected_ids
[x
]);
347 rc
= memcached_flush(memc
,0);
348 test_true(rc
== MEMCACHED_SUCCESS
);
349 return post_udp_op_check(memc
,expected_ids
);
352 static test_return_t
udp_incr_test(memcached_st
*memc
)
354 memcached_return_t rc
;
355 const char *key
= "incr";
356 const char *value
= "1";
357 rc
= memcached_set(memc
, key
, strlen(key
),
358 value
, strlen(value
),
359 (time_t)0, (uint32_t)0);
361 test_true(rc
== MEMCACHED_SUCCESS
);
362 uint16_t *expected_ids
= get_udp_request_ids(memc
);
363 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
364 increment_request_id(&expected_ids
[server_key
]);
366 rc
= memcached_increment(memc
, key
, strlen(key
), 1, &newvalue
);
367 test_true(rc
== MEMCACHED_SUCCESS
);
368 return post_udp_op_check(memc
, expected_ids
);
371 static test_return_t
udp_decr_test(memcached_st
*memc
)
373 memcached_return_t rc
;
374 const char *key
= "decr";
375 const char *value
= "1";
376 rc
= memcached_set(memc
, key
, strlen(key
),
377 value
, strlen(value
),
378 (time_t)0, (uint32_t)0);
380 test_true(rc
== MEMCACHED_SUCCESS
);
381 uint16_t *expected_ids
= get_udp_request_ids(memc
);
382 unsigned int server_key
= memcached_generate_hash(memc
, key
, strlen(key
));
383 increment_request_id(&expected_ids
[server_key
]);
385 rc
= memcached_decrement(memc
, key
, strlen(key
), 1, &newvalue
);
386 test_true(rc
== MEMCACHED_SUCCESS
);
387 return post_udp_op_check(memc
, expected_ids
);
391 static test_return_t
udp_stat_test(memcached_st
*memc
)
393 memcached_stat_st
* rv
= NULL
;
394 memcached_return_t rc
;
396 uint16_t *expected_ids
= get_udp_request_ids(memc
);
397 rv
= memcached_stat(memc
, args
, &rc
);
399 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
400 return post_udp_op_check(memc
, expected_ids
);
403 static test_return_t
udp_version_test(memcached_st
*memc
)
405 memcached_return_t rc
;
406 uint16_t *expected_ids
= get_udp_request_ids(memc
);
407 rc
= memcached_version(memc
);
408 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
409 return post_udp_op_check(memc
, expected_ids
);
412 static test_return_t
udp_get_test(memcached_st
*memc
)
414 memcached_return_t rc
;
415 const char *key
= "foo";
417 uint16_t *expected_ids
= get_udp_request_ids(memc
);
418 char *val
= memcached_get(memc
, key
, strlen(key
), &vlen
, (uint32_t)0, &rc
);
419 test_true(rc
== MEMCACHED_NOT_SUPPORTED
);
420 test_true(val
== NULL
);
421 return post_udp_op_check(memc
, expected_ids
);
424 static test_return_t
udp_mixed_io_test(memcached_st
*memc
)
427 test_st mixed_io_ops
[] ={
429 (test_callback_fn
)udp_set_test
},
430 {"udp_set_too_big_test", 0,
431 (test_callback_fn
)udp_set_too_big_test
},
432 {"udp_delete_test", 0,
433 (test_callback_fn
)udp_delete_test
},
434 {"udp_verbosity_test", 0,
435 (test_callback_fn
)udp_verbosity_test
},
437 (test_callback_fn
)udp_quit_test
},
438 {"udp_flush_test", 0,
439 (test_callback_fn
)udp_flush_test
},
441 (test_callback_fn
)udp_incr_test
},
443 (test_callback_fn
)udp_decr_test
},
444 {"udp_version_test", 0,
445 (test_callback_fn
)udp_version_test
}
448 for (size_t x
= 0; x
< 500; x
++)
450 current_op
= mixed_io_ops
[random() % 9];
451 test_true(current_op
.test_fn(memc
) == TEST_SUCCESS
);
456 test_st udp_setup_server_tests
[] ={
457 {"set_udp_behavior_test", 0, (test_callback_fn
)set_udp_behavior_test
},
458 {"add_tcp_server_udp_client_test", 0, (test_callback_fn
)add_tcp_server_udp_client_test
},
459 {"add_udp_server_tcp_client_test", 0, (test_callback_fn
)add_udp_server_tcp_client_test
},
463 test_st upd_io_tests
[] ={
464 {"udp_set_test", 0, (test_callback_fn
)udp_set_test
},
465 {"udp_buffered_set_test", 0, (test_callback_fn
)udp_buffered_set_test
},
466 {"udp_set_too_big_test", 0, (test_callback_fn
)udp_set_too_big_test
},
467 {"udp_delete_test", 0, (test_callback_fn
)udp_delete_test
},
468 {"udp_buffered_delete_test", 0, (test_callback_fn
)udp_buffered_delete_test
},
469 {"udp_verbosity_test", 0, (test_callback_fn
)udp_verbosity_test
},
470 {"udp_quit_test", 0, (test_callback_fn
)udp_quit_test
},
471 {"udp_flush_test", 0, (test_callback_fn
)udp_flush_test
},
472 {"udp_incr_test", 0, (test_callback_fn
)udp_incr_test
},
473 {"udp_decr_test", 0, (test_callback_fn
)udp_decr_test
},
474 {"udp_stat_test", 0, (test_callback_fn
)udp_stat_test
},
475 {"udp_version_test", 0, (test_callback_fn
)udp_version_test
},
476 {"udp_get_test", 0, (test_callback_fn
)udp_get_test
},
477 {"udp_mixed_io_test", 0, (test_callback_fn
)udp_mixed_io_test
},
481 collection_st collection
[] ={
482 {"udp_setup", (test_callback_fn
)init_udp
, 0, udp_setup_server_tests
},
483 {"udp_io", (test_callback_fn
)init_udp
, 0, upd_io_tests
},
484 {"udp_binary_io", (test_callback_fn
)binary_init_udp
, 0, upd_io_tests
},
488 #define SERVERS_TO_CREATE 5
490 #include "libmemcached_world.h"
492 void get_world(world_st
*world
)
494 world
->collections
= collection
;
496 world
->create
= (test_callback_create_fn
)world_create
;
497 world
->destroy
= (test_callback_fn
)world_destroy
;
499 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
500 world
->test
.flush
= (test_callback_fn
)world_flush
;
501 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
502 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
503 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
505 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
506 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
508 world
->runner
= &defualt_libmemcached_runner
;