Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / tests / failure.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 #include <mem_config.h>
38
39 /*
40 C++ interface test
41 */
42 #include <libmemcached-1.0/memcached.hpp>
43 #include <libmemcached/server_instance.h>
44 #include <libtest/test.hpp>
45
46 #include <cstdio>
47 #include <cstdlib>
48 #include <cstring>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <ctime>
54
55 #include <string>
56 #include <iostream>
57
58 using namespace std;
59 using namespace memcache;
60 using namespace libtest;
61
62 libtest::Framework *global_framework= NULL;
63
64 static test_return_t shutdown_servers(memcached_st *memc)
65 {
66 return TEST_SKIPPED;
67
68 test_skip_valgrind();
69
70 test_compare(memcached_server_count(memc), 1U);
71
72 // Disable a single server, just the first
73 global_framework->servers().shutdown(0);
74
75 return TEST_SUCCESS;
76 }
77
78 static test_return_t add_shutdown_servers(memcached_st *memc)
79 {
80 return TEST_SKIPPED;
81
82 test_skip_valgrind();
83
84 while (memcached_server_count(memc) < 2)
85 {
86 const char *argv[1]= { "add_shutdown_server" };
87 test_true(global_framework->servers().start_socket_server("memcached", libtest::default_port(), 1, argv));
88 test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "localhost", libtest::default_port()));
89 }
90
91 // Disable a single server, just the first
92 global_framework->servers().shutdown(0);
93
94 return TEST_SUCCESS;
95 }
96
97 static test_return_t restart_servers(memcached_st *)
98 {
99 // Restart the servers
100 global_framework->servers().restart();
101
102 return TEST_SUCCESS;
103 }
104
105 #include "libmemcached/instance.hpp"
106 static test_return_t cull_TEST(memcached_st *memc)
107 {
108 uint32_t count= memcached_server_count(memc);
109
110 // Do not do this in your code, it is not supported.
111 memc->servers[0].options.is_dead= true;
112 memc->state.is_time_for_rebuild= true;
113
114 uint32_t new_count= memcached_server_count(memc);
115 test_compare(count, new_count);
116
117 return TEST_SUCCESS;
118 }
119
120 static test_return_t MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST(memcached_st *memc)
121 {
122 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 30));
123 test_compare_got(MEMCACHED_CONNECTION_FAILURE,
124 memcached_set(memc,
125 test_literal_param("foo"),
126 NULL, 0, time_t(0), uint32_t(0)),
127 memcached_last_error_message(memc));
128
129 /*
130 Setting server_failure_counter==0 should not influence the timeout that we set above,
131 since we check the timeout that is created by the failure before we check how many times
132 a server has failed.
133 */
134 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED,
135 memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0)));
136
137 return TEST_SUCCESS;
138 }
139
140 static test_return_t MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST(memcached_st *memc)
141 {
142 return TEST_SKIPPED;
143
144 test_compare_got(MEMCACHED_CONNECTION_FAILURE,
145 memcached_set(memc,
146 test_literal_param("foo"),
147 NULL, 0, time_t(0), uint32_t(0)),
148 memcached_last_error_message(memc));
149
150 /*
151 Setting server_failure_counter==0 should not influence the timeout that we set above,
152 since we check the timeout that is created by the failure before we check how many times
153 a server has failed.
154 */
155 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED,
156 memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0)));
157
158 global_framework->servers().restart();
159
160 int limit= 5;
161 memcached_return_t ret;
162 do {
163 libtest::dream(3, 0);
164 ret= memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0));
165 } while (ret == MEMCACHED_SERVER_TEMPORARILY_DISABLED and --limit);
166
167 test_true(limit);
168
169 test_compare_got(MEMCACHED_SUCCESS, ret, memcached_last_error_message(memc));
170
171 return TEST_SUCCESS;
172 }
173
174 static test_return_t MEMCACHED_SERVER_MARKED_DEAD_TEST(memcached_st *memc)
175 {
176 return TEST_SKIPPED;
177
178 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 30));
179 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS, true));
180
181 memcached_return_t ret;
182 do {
183 ret= memcached_set(memc,
184 test_literal_param("foo"),
185 NULL, 0, time_t(0), uint32_t(0));
186 } while (ret == MEMCACHED_SUCCESS or ret == MEMCACHED_CONNECTION_FAILURE);
187 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED, ret);
188
189 int limit= 5;
190 do {
191 libtest::dream(3, 0);
192 ret= memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0));
193 } while ((ret == MEMCACHED_SERVER_TEMPORARILY_DISABLED or ret == MEMCACHED_SUCCESS) and --limit);
194
195 test_true(limit);
196
197 test_compare_got(MEMCACHED_SERVER_MARKED_DEAD, ret, memcached_last_error_message(memc));
198
199 return TEST_SUCCESS;
200 }
201
202 test_st cull_TESTS[] ={
203 { "cull servers", true, (test_callback_fn*)cull_TEST },
204 { 0, 0, 0 }
205 };
206
207 test_st server_temporarily_disabled_TESTS[] ={
208 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED -> MEMCACHED_SUCCESS)", true, (test_callback_fn*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST },
209 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED)", true, (test_callback_fn*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST },
210 { 0, 0, 0 }
211 };
212
213 test_st server_permanently_disabled_TESTS[] ={
214 { "memcached_set(MEMCACHED_SERVER_MARKED_DEAD)", true, (test_callback_fn*)MEMCACHED_SERVER_MARKED_DEAD_TEST },
215 { 0, 0, 0 }
216 };
217
218 collection_st collection[] ={
219 { "cull", (test_callback_fn*)shutdown_servers, (test_callback_fn*)restart_servers, cull_TESTS },
220 { "server failed", (test_callback_fn*)shutdown_servers, (test_callback_fn*)restart_servers, server_temporarily_disabled_TESTS },
221 { "server eject", (test_callback_fn*)add_shutdown_servers, (test_callback_fn*)restart_servers, server_permanently_disabled_TESTS },
222 { 0, 0, 0, 0 }
223 };
224
225 #include "tests/libmemcached_world.h"
226
227 void get_world(libtest::Framework* world)
228 {
229 world->servers().set_servers_to_run(1);
230
231 world->collections(collection);
232
233 world->create((test_callback_create_fn*)world_create);
234 world->destroy((test_callback_destroy_fn*)world_destroy);
235
236 world->set_runner(new LibmemcachedRunner);
237
238 global_framework= world;
239 }