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