1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "mem_config.h"
43 #include "libmemcached-1.0/memcached.hpp"
44 #include "libtest/test.hpp"
50 #include <sys/types.h>
61 using namespace memcache
;
62 using namespace libtest
;
64 static void populate_vector(vector
<char> &vec
, const string
&str
)
66 vec
.reserve(str
.length());
67 vec
.assign(str
.begin(), str
.end());
70 static void copy_vec_to_string(vector
<char> &vec
, string
&str
)
75 str
.assign(vec
.begin(), vec
.end());
79 static test_return_t
basic_test(memcached_st
*memc
)
82 const string
value_set("This is some data");
83 std::vector
<char> value
;
84 std::vector
<char> test_value
;
86 populate_vector(value
, value_set
);
88 test_true(foo
.set("mine", value
, 0, 0));
89 test_true(foo
.get("mine", test_value
));
91 test_compare(test_value
.size(), value
.size());
92 test_memcmp(&test_value
[0], &value
[0], test_value
.size());
93 test_false(foo
.set("", value
, 0, 0));
98 static test_return_t
increment_test(memcached_st
*original
)
100 Memcache
mcach(original
);
101 const string
key("blah");
102 const string
inc_value("1");
103 std::vector
<char> inc_val
;
104 vector
<char> ret_value
;
106 uint64_t int_inc_value
;
107 uint64_t int_ret_value
;
109 populate_vector(inc_val
, inc_value
);
111 test_true(mcach
.set(key
, inc_val
, 0, 0));
113 test_true(mcach
.get(key
, ret_value
));
114 test_false(ret_value
.empty());
115 copy_vec_to_string(ret_value
, ret_string
);
117 int_inc_value
= uint64_t(atol(inc_value
.c_str()));
118 int_ret_value
= uint64_t(atol(ret_string
.c_str()));
119 test_compare(int_inc_value
, int_ret_value
);
121 test_true(mcach
.increment(key
, 1, &int_ret_value
));
122 test_compare(uint64_t(2), int_ret_value
);
124 test_true(mcach
.increment(key
, 1, &int_ret_value
));
125 test_compare(uint64_t(3), int_ret_value
);
127 test_true(mcach
.increment(key
, 5, &int_ret_value
));
128 test_compare(uint64_t(8), int_ret_value
);
133 static test_return_t
basic_master_key_test(memcached_st
*original
)
135 Memcache
foo(original
);
136 const string
value_set("Data for server A");
138 vector
<char> test_value
;
139 const string
master_key_a("server-a");
140 const string
master_key_b("server-b");
141 const string
key("xyz");
143 populate_vector(value
, value_set
);
145 test_true(foo
.setByKey(master_key_a
, key
, value
, 0, 0));
146 test_true(foo
.getByKey(master_key_a
, key
, test_value
));
148 test_compare(value
.size(), test_value
.size());
149 test_memcmp(&value
[0], &test_value
[0], value
.size());
154 test_false(foo
.getByKey(master_key_b
, key
, test_value
));
155 test_zero(test_value
.size());
161 static test_return_t
mget_test(memcached_st
*original
)
163 Memcache
memc(original
);
164 memcached_return_t mc_rc
;
166 vector
< vector
<char> *> values
;
168 keys
.push_back("fudge");
169 keys
.push_back("son");
170 keys
.push_back("food");
174 populate_vector(val1
, "fudge");
175 populate_vector(val2
, "son");
176 populate_vector(val3
, "food");
178 values
.push_back(&val1
);
179 values
.push_back(&val2
);
180 values
.push_back(&val3
);
183 vector
<char> return_value
;
185 /* We need to empty the server before we continue the test */
186 bool flush_res
= memc
.flush();
187 if (flush_res
== false)
189 std::string error_string
;
190 ASSERT_TRUE(memc
.error(error_string
));
191 Error
<< error_string
;
193 test_true(memc
.flush());
195 test_true(memc
.mget(keys
));
197 test_compare(MEMCACHED_NOTFOUND
,
198 memc
.fetch(return_key
, return_value
));
200 test_true(memc
.setAll(keys
, values
, 50, 9));
202 test_true(memc
.mget(keys
));
204 while (memcached_success(mc_rc
= memc
.fetch(return_key
, return_value
)))
206 test_compare(return_key
.length(), return_value
.size());
207 test_memcmp(&return_value
[0], return_key
.c_str(), return_value
.size());
210 test_compare(values
.size(), count
);
215 static test_return_t
lp_1010899_TEST(void*)
217 // Check to see everything is setup internally even when no initial hosts are
221 test_false(memc
.increment(__func__
, 0, NULL
));
226 static test_return_t
lp_1010899_with_args_TEST(memcached_st
*original
)
228 // Check to see everything is setup internally even when a host is specified
230 const memcached_instance_st
* instance
= memcached_server_instance_by_position(original
, 0);
231 Memcache
memc(memcached_server_name(instance
), memcached_server_port(instance
));
233 test_false(memc
.increment(__func__
, 0, NULL
));
234 test_true(memc
.set(__func__
, test_literal_param("12"), 0, 0));
235 test_true(memc
.increment(__func__
, 3, NULL
));
237 std::vector
<char> ret_val
;
238 test_true(memc
.get(__func__
, ret_val
));
240 test_strcmp(&ret_val
[0], "15");
245 static test_return_t
basic_behavior(memcached_st
*original
)
247 Memcache
memc(original
);
248 test_true(memc
.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
, true));
249 test_compare(true, memc
.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY
));
254 static test_return_t
error_test(memcached_st
*)
256 Memcache
memc("--server=localhost:178");
257 std::vector
<char> value
;
259 test_false(memc
.set("key", value
, time_t(0), uint32_t(0)));
261 test_true(memc
.error());
266 static test_return_t
error_std_string_test(memcached_st
*)
268 Memcache
memc("--server=localhost:178");
269 std::vector
<char> value
;
271 test_false(memc
.set("key", value
, time_t(0), uint32_t(0)));
273 std::string error_message
;
274 test_true(memc
.error(error_message
));
275 test_false(error_message
.empty());
280 static test_return_t
error_memcached_return_t_test(memcached_st
*)
282 Memcache
memc("--server=localhost:178");
283 std::vector
<char> value
;
285 test_false(memc
.set("key", value
, time_t(0), uint32_t(0)));
287 memcached_return_t rc
;
288 test_true(memc
.error(rc
));
289 test_compare(MEMCACHED_CONNECTION_FAILURE
, rc
);
294 test_st error_tests
[] ={
295 { "error()", false, reinterpret_cast<test_callback_fn
*>(error_test
) },
296 { "error(std::string&)", false, reinterpret_cast<test_callback_fn
*>(error_std_string_test
) },
297 { "error(memcached_return_t&)", false, reinterpret_cast<test_callback_fn
*>(error_memcached_return_t_test
) },
303 reinterpret_cast<test_callback_fn
*>(basic_test
) },
304 { "basic_master_key", false,
305 reinterpret_cast<test_callback_fn
*>(basic_master_key_test
) },
306 { "increment_test", false,
307 reinterpret_cast<test_callback_fn
*>(increment_test
) },
309 reinterpret_cast<test_callback_fn
*>(mget_test
) },
310 { "basic_behavior", false,
311 reinterpret_cast<test_callback_fn
*>(basic_behavior
) },
315 test_st regression_TESTS
[] ={
316 { "lp:1010899 Memcache()", false, lp_1010899_TEST
},
317 { "lp:1010899 Memcache(localhost, port)", false,
318 reinterpret_cast<test_callback_fn
*>(lp_1010899_with_args_TEST
) },
322 collection_st collection
[] ={
323 {"block", 0, 0, tests
},
324 {"error()", 0, 0, error_tests
},
325 {"regression", 0, 0, regression_TESTS
},
329 #define SERVERS_TO_CREATE 5
331 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10
332 #include "tests/libmemcached_world.h"
334 void get_world(libtest::Framework
* world
)
336 world
->collections(collection
);
338 world
->create((test_callback_create_fn
*)world_create
);
339 world
->destroy((test_callback_destroy_fn
*)world_destroy
);
341 world
->set_runner(new LibmemcachedRunner
);