Merge in updates for server failure testing.
[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/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_compare(memcached_server_count(memc), 1U);
67
68 // Disable a single server, just the first
69 global_framework->servers().shutdown(0);
70
71 return TEST_SUCCESS;
72 }
73
74 static test_return_t restart_servers(memcached_st *)
75 {
76 // Restart the servers
77 global_framework->servers().restart();
78
79 return TEST_SUCCESS;
80 }
81
82 static test_return_t cull_TEST(memcached_st *memc)
83 {
84 uint32_t count= memcached_server_count(memc);
85
86 // Do not do this in your code, it is not supported.
87 memc->servers[0].options.is_dead= true;
88 memc->state.is_time_for_rebuild= true;
89
90 uint32_t new_count= memcached_server_count(memc);
91 test_compare(count, new_count);
92
93 return TEST_SUCCESS;
94 }
95
96 static test_return_t MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST(memcached_st *memc)
97 {
98 test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 30));
99 test_compare_got(MEMCACHED_CONNECTION_FAILURE,
100 memcached_set(memc,
101 test_literal_param("foo"),
102 NULL, 0, time_t(0), uint32_t(0)),
103 memcached_last_error_message(memc));
104
105 /*
106 Setting server_failure_counter==0 should not influence the timeout that we set above,
107 since we check the timeout that is created by the failure before we check how many times
108 a server has failed.
109 */
110 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED,
111 memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0)));
112
113 return TEST_SUCCESS;
114 }
115
116 static test_return_t MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST(memcached_st *memc)
117 {
118 test_compare_got(MEMCACHED_CONNECTION_FAILURE,
119 memcached_set(memc,
120 test_literal_param("foo"),
121 NULL, 0, time_t(0), uint32_t(0)),
122 memcached_last_error_message(memc));
123
124 /*
125 Setting server_failure_counter==0 should not influence the timeout that we set above,
126 since we check the timeout that is created by the failure before we check how many times
127 a server has failed.
128 */
129 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED,
130 memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0)));
131
132 global_framework->servers().restart();
133
134 memcached_return_t ret;
135 do {
136 sleep(3);
137 ret= memcached_set(memc, test_literal_param("foo"), NULL, 0, time_t(0), uint32_t(0));
138 } while (ret == MEMCACHED_SERVER_TEMPORARILY_DISABLED);
139
140 test_compare_got(MEMCACHED_SUCCESS, ret, memcached_last_error_message(memc));
141
142 return TEST_SUCCESS;
143 }
144
145 test_st cull_TESTS[] ={
146 { "cull servers", true, (test_callback_fn*)cull_TEST },
147 { 0, 0, 0 }
148 };
149
150 test_st server_temporarily_disabled_TESTS[] ={
151 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED)", true, (test_callback_fn*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST },
152 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED -> MEMCACHED_SUCCESS)", true, (test_callback_fn*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST },
153 { 0, 0, 0 }
154 };
155
156 collection_st collection[] ={
157 { "cull", (test_callback_fn*)shutdown_servers, (test_callback_fn*)restart_servers, cull_TESTS },
158 { "server failed", (test_callback_fn*)shutdown_servers, (test_callback_fn*)restart_servers, server_temporarily_disabled_TESTS },
159 { 0, 0, 0, 0 }
160 };
161
162 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
163 #include "libmemcached_world.h"
164
165 void get_world(Framework *world)
166 {
167 world->servers().set_count(1);
168
169 world->collections= collection;
170
171 world->_create= (test_callback_create_fn*)world_create;
172 world->_destroy= (test_callback_destroy_fn*)world_destroy;
173
174 world->item._startup= (test_callback_fn*)world_test_startup;
175 world->item.set_pre((test_callback_fn*)world_pre_run);
176 world->item.set_flush((test_callback_fn*)world_flush);
177 world->item.set_post((test_callback_fn*)world_post_run);
178 world->_on_error= (test_callback_error_fn*)world_on_error;
179
180 world->collection_startup= (test_callback_fn*)world_container_startup;
181 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
182
183 world->set_runner(&defualt_libmemcached_runner);
184
185 global_framework= world;
186 }