Updating to latest libtest.
[m6w6/libmemcached] / tests / cycle.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37
38 /*
39 Test that we are cycling the servers we are creating during testing.
40 */
41
42 #include <config.h>
43 #include <libtest/test.hpp>
44
45 #include <libmemcached/common.h>
46 #include <libmemcached/is.h>
47 #include <libmemcached/util.h>
48
49 #include <iostream>
50
51
52 #include <libtest/server.h>
53
54 using namespace libtest;
55
56 #define SERVERS_TO_CREATE 5
57
58 #ifndef __INTEL_COMPILER
59 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
60 #endif
61
62 static test_return_t alive(memcached_st *memc)
63 {
64 test_true(memc);
65 test_true(memcached_is_allocated(memc));
66 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
67 {
68 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
69 test_true(instance);
70
71 test_true(libmemcached_util_ping(memcached_server_name(instance),
72 memcached_server_port(instance), NULL));
73 }
74
75 return TEST_SUCCESS;
76 }
77
78 static test_return_t valid(memcached_st *memc)
79 {
80 test_true(memc);
81 test_true(memcached_is_allocated(memc));
82
83 for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
84 {
85 memcached_server_instance_st instance= memcached_server_instance_by_position(memc, x);
86 test_true(instance);
87
88 pid_t pid= libmemcached_util_getpid(memcached_server_name(instance),
89 memcached_server_port(instance), NULL);
90 test_true(pid != -1);
91 }
92
93 return TEST_SUCCESS;
94 }
95
96 static test_return_t kill_test(memcached_st *)
97 {
98 static struct timespec global_sleep_value= { 2, 0 };
99
100 #ifdef WIN32
101 sleep(1);
102 #else
103 nanosleep(&global_sleep_value, NULL);
104 #endif
105
106 return TEST_SUCCESS;
107 }
108
109 test_st ping_tests[] ={
110 {"alive", true, (test_callback_fn*)alive },
111 {0, 0, 0}
112 };
113
114 test_st getpid_tests[] ={
115 {"valid", true, (test_callback_fn*)valid },
116 {0, 0, 0}
117 };
118
119 test_st kill_tests[] ={
120 {"kill", true, (test_callback_fn*)kill_test },
121 {0, 0, 0}
122 };
123
124 collection_st collection[] ={
125 {"libmemcached_util_ping()", 0, 0, ping_tests},
126 {"libmemcached_util_getpid()", 0, 0, getpid_tests},
127 {"kill", 0, 0, kill_tests},
128 {0, 0, 0, 0}
129 };
130
131
132 #include "tests/libmemcached_world.h"
133
134 void get_world(Framework *world)
135 {
136 world->collections= collection;
137
138 world->_create= (test_callback_create_fn*)world_create;
139 world->_destroy= (test_callback_destroy_fn*)world_destroy;
140
141 world->item.set_startup((test_callback_fn*)world_test_startup);
142 world->item.set_pre((test_callback_fn*)world_pre_run);
143 world->item.set_post((test_callback_fn*)world_post_run);
144
145 world->set_on_error((test_callback_error_fn*)world_on_error);
146
147 world->collection_startup= (test_callback_fn*)world_container_startup;
148 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
149
150 world->set_runner(&defualt_libmemcached_runner);
151 }
152