1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
42 #include <libmemcached-1.0/memcached.hpp>
43 #include <libmemcached/server_instance.h>
44 #include <libtest/test.hpp>
50 #include <sys/types.h>
59 using namespace memcache
;
60 using namespace libtest
;
62 Framework
*global_framework
= NULL
;
64 static test_return_t
shutdown_servers(memcached_st
*memc
)
66 test_compare(memcached_server_count(memc
), 1U);
68 // Disable a single server, just the first
69 global_framework
->servers().shutdown(0);
74 static test_return_t
add_shutdown_servers(memcached_st
*memc
)
76 while (memcached_server_count(memc
) < 2)
78 const char *argv
[1]= { "add_shutdown_server" };
79 in_port_t port
= max_port() +1;
80 test_true(global_framework
->servers().start_socket_server("memcached", port
, 1, argv
));
81 test_compare(MEMCACHED_SUCCESS
, memcached_server_add(memc
, "localhost", port
));
84 // Disable a single server, just the first
85 global_framework
->servers().shutdown(0);
90 static test_return_t
restart_servers(memcached_st
*)
92 // Restart the servers
93 global_framework
->servers().restart();
98 static test_return_t
cull_TEST(memcached_st
*memc
)
100 uint32_t count
= memcached_server_count(memc
);
102 // Do not do this in your code, it is not supported.
103 memc
->servers
[0].options
.is_dead
= true;
104 memc
->state
.is_time_for_rebuild
= true;
106 uint32_t new_count
= memcached_server_count(memc
);
107 test_compare(count
, new_count
);
112 static test_return_t
MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST(memcached_st
*memc
)
114 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 30));
115 test_compare_got(MEMCACHED_CONNECTION_FAILURE
,
117 test_literal_param("foo"),
118 NULL
, 0, time_t(0), uint32_t(0)),
119 memcached_last_error_message(memc
));
122 Setting server_failure_counter==0 should not influence the timeout that we set above,
123 since we check the timeout that is created by the failure before we check how many times
126 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED
,
127 memcached_set(memc
, test_literal_param("foo"), NULL
, 0, time_t(0), uint32_t(0)));
132 static test_return_t
MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST(memcached_st
*memc
)
134 test_compare_got(MEMCACHED_CONNECTION_FAILURE
,
136 test_literal_param("foo"),
137 NULL
, 0, time_t(0), uint32_t(0)),
138 memcached_last_error_message(memc
));
141 Setting server_failure_counter==0 should not influence the timeout that we set above,
142 since we check the timeout that is created by the failure before we check how many times
145 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED
,
146 memcached_set(memc
, test_literal_param("foo"), NULL
, 0, time_t(0), uint32_t(0)));
148 global_framework
->servers().restart();
150 memcached_return_t ret
;
153 ret
= memcached_set(memc
, test_literal_param("foo"), NULL
, 0, time_t(0), uint32_t(0));
154 } while (ret
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
);
156 test_compare_got(MEMCACHED_SUCCESS
, ret
, memcached_last_error_message(memc
));
161 static test_return_t
MEMCACHED_SERVER_MARKED_DEAD_TEST(memcached_st
*memc
)
163 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT
, 30));
164 test_compare(MEMCACHED_SUCCESS
, memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
, true));
166 memcached_return_t ret
;
168 ret
= memcached_set(memc
,
169 test_literal_param("foo"),
170 NULL
, 0, time_t(0), uint32_t(0));
171 } while (ret
== MEMCACHED_SUCCESS
or ret
== MEMCACHED_CONNECTION_FAILURE
);
172 test_compare(MEMCACHED_SERVER_TEMPORARILY_DISABLED
, ret
);
176 ret
= memcached_set(memc
, test_literal_param("foo"), NULL
, 0, time_t(0), uint32_t(0));
177 } while (ret
== MEMCACHED_SERVER_TEMPORARILY_DISABLED
or ret
== MEMCACHED_SUCCESS
);
179 test_compare_got(MEMCACHED_SERVER_MARKED_DEAD
, ret
, memcached_last_error_message(memc
));
184 test_st cull_TESTS
[] ={
185 { "cull servers", true, (test_callback_fn
*)cull_TEST
},
189 test_st server_temporarily_disabled_TESTS
[] ={
190 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED)", true, (test_callback_fn
*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_TEST
},
191 { "memcached_set(MEMCACHED_SERVER_TEMPORARILY_DISABLED -> MEMCACHED_SUCCESS)", true, (test_callback_fn
*)MEMCACHED_SERVER_TEMPORARILY_DISABLED_to_success_TEST
},
195 test_st server_permanently_disabled_TESTS
[] ={
196 { "memcached_set(MEMCACHED_SERVER_MARKED_DEAD)", true, (test_callback_fn
*)MEMCACHED_SERVER_MARKED_DEAD_TEST
},
200 collection_st collection
[] ={
201 { "cull", (test_callback_fn
*)shutdown_servers
, (test_callback_fn
*)restart_servers
, cull_TESTS
},
202 { "server failed", (test_callback_fn
*)shutdown_servers
, (test_callback_fn
*)restart_servers
, server_temporarily_disabled_TESTS
},
203 { "server eject", (test_callback_fn
*)add_shutdown_servers
, (test_callback_fn
*)restart_servers
, server_permanently_disabled_TESTS
},
207 #include "libmemcached_world.h"
209 void get_world(Framework
*world
)
211 world
->servers().set_count(1);
213 world
->collections
= collection
;
215 world
->_create
= (test_callback_create_fn
*)world_create
;
216 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
218 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
219 world
->item
.set_pre((test_callback_fn
*)world_pre_run
);
220 world
->item
.set_flush((test_callback_fn
*)world_flush
);
221 world
->item
.set_post((test_callback_fn
*)world_post_run
);
222 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
224 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
225 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
227 world
->set_runner(&defualt_libmemcached_runner
);
229 global_framework
= world
;