1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
6
* Copyright (C) 2006-2009 Brian Aker All rights reserved.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
Sample test application.
44
#include <libtest/test.hpp>
46
using namespace libtest;
48
#include <libmemcached-1.0/memcached.h>
49
#include <libmemcached/server_instance.h>
50
#include <libmemcached/io.h>
51
#include <libmemcached/udp.hpp>
52
#include <libmemcachedutil-1.0/util.h>
58
#include <sys/types.h>
64
#include <libtest/server.h>
66
#ifndef __INTEL_COMPILER
67
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
71
@note This should be testing to see if the server really supports the binary protocol.
73
static test_return_t pre_binary(memcached_st *memc)
75
memcached_st *memc_clone= memcached_clone(NULL, memc);
76
test_true(memc_clone);
78
// The memcached_version needs to be done on a clone, because the server
79
// will not toggle protocol on an connection.
80
memcached_version(memc_clone);
82
test_compare(MEMCACHED_SUCCESS, memcached_version(memc));
83
test_compare(true, libmemcached_util_version_check(memc, 1, 2, 1));
84
test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, true));
85
test_compare(true, memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL));
87
memcached_free(memc_clone);
92
typedef std::vector<uint16_t> Expected;
94
static void increment_request_id(uint16_t *id)
97
if ((*id & UDP_REQUEST_ID_THREAD_MASK) != 0)
103
static void get_udp_request_ids(memcached_st *memc, Expected &ids)
105
for (uint32_t x= 0; x < memcached_server_count(memc); x++)
107
memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
109
ids.push_back(get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st )instance)->write_buffer));
113
static test_return_t post_udp_op_check(memcached_st *memc, Expected& expected_req_ids)
116
(void)expected_req_ids;
118
memcached_server_st *cur_server = memcached_server_list(memc);
119
uint16_t *cur_req_ids = get_udp_request_ids(memc);
121
for (size_t x= 0; x < memcached_server_count(memc); x++)
123
test_true(cur_server[x].cursor_active == 0);
124
test_true(cur_req_ids[x] == expected_req_ids[x]);
126
free(expected_req_ids);
134
** There is a little bit of a hack here, instead of removing
135
** the servers, I just set num host to 0 and them add then new udp servers
137
static test_return_t init_udp(memcached_st *memc)
139
test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
144
static test_return_t init_udp_valgrind(memcached_st *memc)
146
if (getenv("TESTS_ENVIRONMENT"))
151
test_skip(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
156
static test_return_t binary_init_udp(memcached_st *memc)
158
if (getenv("TESTS_ENVIRONMENT"))
163
test_skip(TEST_SUCCESS, pre_binary(memc));
165
return init_udp(memc);
168
/* Make sure that I cant add a tcp server to a udp client */
169
static test_return_t add_tcp_server_udp_client_test(memcached_st *memc)
173
memcached_server_st server;
174
memcached_server_instance_st instance=
175
memcached_server_instance_by_position(memc, 0);
176
memcached_server_clone(&server, &memc->hosts[0]);
177
test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
178
test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
183
/* Make sure that I cant add a udp server to a tcp client */
184
static test_return_t add_udp_server_tcp_client_test(memcached_st *memc)
188
memcached_server_st server;
189
memcached_server_instance_st instance=
190
memcached_server_instance_by_position(memc, 0);
191
memcached_server_clone(&server, &memc->hosts[0]);
192
test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
194
memcached_st tcp_client;
195
memcached_create(&tcp_client);
196
test_true(memcached_server_add_udp(&tcp_client, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
202
static test_return_t version_TEST(memcached_st *memc)
204
test_compare(MEMCACHED_NOT_SUPPORTED, memcached_version(memc));
208
static test_return_t verbosity_TEST(memcached_st *memc)
210
test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 0));
214
static test_return_t memcached_get_TEST(memcached_st *memc)
216
memcached_return_t rc;
217
test_null(memcached_get(memc,
218
test_literal_param(__func__),
220
test_compare(MEMCACHED_NOT_SUPPORTED, rc);
225
static test_return_t memcached_mget_execute_by_key_TEST(memcached_st *memc)
228
size_t *key_length= NULL;
229
test_compare(MEMCACHED_NOT_SUPPORTED,
230
memcached_mget_execute_by_key(memc,
231
test_literal_param(__func__), // Group key
232
keys, key_length, // Actual key
236
0)); // Number of callbacks
241
static test_return_t memcached_stat_TEST(memcached_st *memc)
243
memcached_return_t rc;
244
test_null(memcached_stat(memc, 0, &rc));
245
test_compare(MEMCACHED_NOT_SUPPORTED, rc);
250
static test_return_t set_udp_behavior_test(memcached_st *memc)
252
memcached_quit(memc);
254
test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution));
255
test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, true));
256
test_compare(true, memc->flags.use_udp);
257
test_compare(false, memc->flags.reply);
259
test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, false));
260
test_compare(false, memc->flags.use_udp);
261
test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, false));
262
test_compare(true, memc->flags.reply);
267
static test_return_t udp_set_test(memcached_st *memc)
269
// Assume we are running under valgrind, and bail
270
if (getenv("TESTS_ENVIRONMENT"))
275
const unsigned int num_iters= 1025; //request id rolls over at 1024
279
for (size_t x= 0; x < num_iters;x++)
281
Expected expected_ids;
282
get_udp_request_ids(memc, expected_ids);
283
unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
284
test_true(server_key < memcached_server_count(memc));
285
memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
286
size_t init_offset= instance->write_buffer_offset;
288
test_compare_hint(MEMCACHED_SUCCESS,
290
test_literal_param("foo"),
291
test_literal_param("when we sanitize"),
292
time_t(0), uint32_t(0)),
293
memcached_last_error_message(memc));
296
NB, the check below assumes that if new write_ptr is less than
297
the original write_ptr that we have flushed. For large payloads, this
298
maybe an invalid assumption, but for the small payload we have it is OK
300
if (instance->write_buffer_offset < init_offset)
302
increment_request_id(&expected_ids[server_key]);
305
test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
311
static test_return_t udp_buffered_set_test(memcached_st *memc)
314
test_compare(MEMCACHED_INVALID_ARGUMENTS,
315
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true));
319
static test_return_t udp_set_too_big_test(memcached_st *memc)
322
Expected expected_ids;
323
get_udp_request_ids(memc, expected_ids);
325
std::vector<char> value;
326
value.resize(1024 * 1024 * 10);
328
test_compare_hint(MEMCACHED_WRITE_FAILURE,
330
test_literal_param(__func__),
331
&value[0], value.size(),
332
time_t(0), uint32_t(0)),
333
memcached_last_error_message(memc));
334
memcached_quit(memc);
336
return post_udp_op_check(memc, expected_ids);
339
static test_return_t udp_delete_test(memcached_st *memc)
343
//request id rolls over at 1024
344
for (size_t x= 0; x < 1025; x++)
346
Expected expected_ids;
347
get_udp_request_ids(memc, expected_ids);
349
unsigned int server_key= memcached_generate_hash(memc, test_literal_param("foo"));
350
memcached_server_instance_st instance= memcached_server_instance_by_position(memc, server_key);
351
size_t init_offset= instance->write_buffer_offset;
353
test_compare(MEMCACHED_SUCCESS,
354
memcached_delete(memc, test_literal_param("foo"), 0));
356
if (instance->write_buffer_offset < init_offset)
358
increment_request_id(&expected_ids[server_key]);
361
test_compare(TEST_SUCCESS, post_udp_op_check(memc, expected_ids));
367
static test_return_t udp_buffered_delete_test(memcached_st *memc)
369
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
370
return udp_delete_test(memc);
373
static test_return_t udp_verbosity_test(memcached_st *memc)
375
Expected expected_ids;
376
get_udp_request_ids(memc, expected_ids);
378
for (size_t x= 0; x < memcached_server_count(memc); x++)
380
increment_request_id(&expected_ids[x]);
383
test_compare(MEMCACHED_SUCCESS, memcached_verbosity(memc, 3));
385
return post_udp_op_check(memc, expected_ids);
388
static test_return_t udp_quit_test(memcached_st *memc)
390
Expected expected_ids;
391
memcached_quit(memc);
393
return post_udp_op_check(memc, expected_ids);
396
static test_return_t udp_flush_test(memcached_st *memc)
398
Expected expected_ids;
399
get_udp_request_ids(memc, expected_ids);
401
for (size_t x= 0; x < memcached_server_count(memc); x++)
403
increment_request_id(&expected_ids[x]);
405
memcached_error_print(memc);
406
test_compare_hint(MEMCACHED_SUCCESS, memcached_flush(memc, 0), memcached_last_error_message(memc));
408
return post_udp_op_check(memc, expected_ids);
411
static test_return_t udp_incr_test(memcached_st *memc)
413
test_compare(MEMCACHED_SUCCESS,
414
memcached_set(memc, test_literal_param("incr"),
415
test_literal_param("1"),
416
(time_t)0, (uint32_t)0));
418
Expected expected_ids;
419
get_udp_request_ids(memc, expected_ids);
421
unsigned int server_key= memcached_generate_hash(memc, test_literal_param("incr"));
422
increment_request_id(&expected_ids[server_key]);
425
test_compare(MEMCACHED_SUCCESS, memcached_increment(memc, test_literal_param("incr"), 1, &newvalue));
427
return post_udp_op_check(memc, expected_ids);
430
static test_return_t udp_decr_test(memcached_st *memc)
432
test_compare(MEMCACHED_SUCCESS,
434
test_literal_param(__func__),
435
test_literal_param("1"),
436
time_t(0), uint32_t(0)));
438
Expected expected_ids;
439
get_udp_request_ids(memc, expected_ids);
441
unsigned int server_key= memcached_generate_hash(memc,
442
test_literal_param(__func__));
443
increment_request_id(&expected_ids[server_key]);
446
test_compare(MEMCACHED_SUCCESS, memcached_decrement(memc,
447
test_literal_param(__func__),
450
return post_udp_op_check(memc, expected_ids);
454
static test_return_t udp_stat_test(memcached_st *memc)
456
memcached_return_t rc;
458
Expected expected_ids;
459
get_udp_request_ids(memc, expected_ids);
460
memcached_stat_st *rv= memcached_stat(memc, args, &rc);
461
memcached_stat_free(memc, rv);
462
test_compare(MEMCACHED_NOT_SUPPORTED, rc);
464
return post_udp_op_check(memc, expected_ids);
467
static test_return_t udp_version_test(memcached_st *memc)
469
Expected expected_ids;
470
get_udp_request_ids(memc, expected_ids);
472
test_compare(MEMCACHED_NOT_SUPPORTED,
473
memcached_version(memc));
475
return post_udp_op_check(memc, expected_ids);
478
static test_return_t udp_get_test(memcached_st *memc)
480
memcached_return_t rc;
482
Expected expected_ids;
483
get_udp_request_ids(memc, expected_ids);
484
test_null(memcached_get(memc, test_literal_param("foo"), &vlen, (uint32_t)0, &rc));
485
test_compare(MEMCACHED_NOT_SUPPORTED, rc);
487
return post_udp_op_check(memc, expected_ids);
490
static test_return_t udp_mixed_io_test(memcached_st *memc)
492
test_st mixed_io_ops [] ={
494
(test_callback_fn*)udp_set_test},
495
{"udp_set_too_big_test", 0,
496
(test_callback_fn*)udp_set_too_big_test},
497
{"udp_delete_test", 0,
498
(test_callback_fn*)udp_delete_test},
499
{"udp_verbosity_test", 0,
500
(test_callback_fn*)udp_verbosity_test},
502
(test_callback_fn*)udp_quit_test},
504
{"udp_flush_test", 0,
505
(test_callback_fn*)udp_flush_test},
508
(test_callback_fn*)udp_incr_test},
510
(test_callback_fn*)udp_decr_test},
511
{"udp_version_test", 0,
512
(test_callback_fn*)udp_version_test}
515
for (size_t x= 0; x < 500; x++)
517
test_st current_op= mixed_io_ops[(random() % 8)];
518
test_compare(TEST_SUCCESS, current_op.test_fn(memc));
523
test_st compatibility_TESTS[] ={
524
{"version", 0, (test_callback_fn*)version_TEST },
525
{"version", 0, (test_callback_fn*)verbosity_TEST },
526
{"memcached_get()", 0, (test_callback_fn*)memcached_get_TEST },
527
{"memcached_mget_execute_by_key()", 0, (test_callback_fn*)memcached_mget_execute_by_key_TEST },
528
{"memcached_stat()", 0, (test_callback_fn*)memcached_stat_TEST },
532
test_st udp_setup_server_tests[] ={
533
{"set_udp_behavior_test", 0, (test_callback_fn*)set_udp_behavior_test},
534
{"add_tcp_server_udp_client_test", 0, (test_callback_fn*)add_tcp_server_udp_client_test},
535
{"add_udp_server_tcp_client_test", 0, (test_callback_fn*)add_udp_server_tcp_client_test},
539
test_st upd_io_tests[] ={
540
{"udp_set_test", 0, (test_callback_fn*)udp_set_test},
541
{"udp_buffered_set_test", 0, (test_callback_fn*)udp_buffered_set_test},
542
{"udp_set_too_big_test", 0, (test_callback_fn*)udp_set_too_big_test},
543
{"udp_delete_test", 0, (test_callback_fn*)udp_delete_test},
544
{"udp_buffered_delete_test", 0, (test_callback_fn*)udp_buffered_delete_test},
545
{"udp_verbosity_test", 0, (test_callback_fn*)udp_verbosity_test},
546
{"udp_quit_test", 0, (test_callback_fn*)udp_quit_test},
547
{"udp_flush_test", 0, (test_callback_fn*)udp_flush_test},
548
{"udp_incr_test", 0, (test_callback_fn*)udp_incr_test},
549
{"udp_decr_test", 0, (test_callback_fn*)udp_decr_test},
550
{"udp_stat_test", 0, (test_callback_fn*)udp_stat_test},
551
{"udp_version_test", 0, (test_callback_fn*)udp_version_test},
552
{"udp_get_test", 0, (test_callback_fn*)udp_get_test},
553
{"udp_mixed_io_test", 0, (test_callback_fn*)udp_mixed_io_test},
557
collection_st collection[] ={
558
{"udp_setup", (test_callback_fn*)init_udp, 0, udp_setup_server_tests},
559
{"compatibility", (test_callback_fn*)init_udp, 0, compatibility_TESTS},
560
{"udp_io", (test_callback_fn*)init_udp_valgrind, 0, upd_io_tests},
561
{"udp_binary_io", (test_callback_fn*)binary_init_udp, 0, upd_io_tests},
565
#include "tests/libmemcached_world.h"
567
void get_world(Framework *world)
569
world->collections= collection;
571
world->_create= (test_callback_create_fn*)world_create;
572
world->_destroy= (test_callback_destroy_fn*)world_destroy;
574
world->item._startup= (test_callback_fn*)world_test_startup;
575
world->item._flush= (test_callback_fn*)world_flush;
576
world->item.set_pre((test_callback_fn*)world_pre_run);
577
world->item.set_post((test_callback_fn*)world_post_run);
578
world->_on_error= (test_callback_error_fn*)world_on_error;
580
world->collection_startup= (test_callback_fn*)world_container_startup;
581
world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
583
world->set_runner(&defualt_libmemcached_runner);