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